动态使用函数的次数

发布于 2024-12-23 11:20:15 字数 464 浏览 3 评论 0原文

可能的重复:
如何循环动态表单输入和插入数组

我有一个 php 脚本和一个表单。 php 脚本创建一个 xml 文件,但我需要的是有人输入一个数字,这将设置一定数量的文本框,以便有人为该 xml 文件写入数据。
所以我需要它编写 无论用户输入多少次。此外,名称必须是数字,但按 1 计数,例如: ... 谢谢

Possible Duplicate:
How to loop through dynamic form inputs and insert into an array

I have a php script and a form. The php script makes an xml file but what i need is for someone to enter a number and that would set that amount of textboxes that would be for someone to write data for that xml file.
So i need it to write <input type="text" name="a #"> however many times the user enters. Also the name needs to be a number but it counts by one ex:<input type="text" name="1"> <input type="text" name="2">... Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绾颜 2024-12-30 11:20:15
<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
    $count = $_POST['quantity'];
    for ($i=1; $i<=$count; $i++){
        @$s.= "<input type=text name=".$i."><br>";
    }
?>

现在只需在 html 中回显 $s

<?php
session_start();
if(isset($_POST['quantity']){
// code here to check isnum and min/max
    $count = $_POST['quantity'];
    for ($i=1; $i<=$count; $i++){
        @$s.= "<input type=text name=".$i."><br>";
    }
?>

now just echo out $s in your html

南烟 2024-12-30 11:20:15

这?

<form method="get" action="">
    <div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>

<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>

<form method="post" action="">
    <?php for ($i = 0; $i < $num_inputs; $i++) : ?>
        <div><input type="text" name="inputs[]"/></div>
    <?php endfor ?>
</form>

编辑:是的,数组比 input_x 好得多。更新了我的答案。

This?

<form method="get" action="">
    <div><input type="text" name="num_inputs" value="1" placeholder="Number of inputs"/></div>
</form>

<?php $num_inputs = isset($_GET['num_inputs']) ? $_GET['num_inputs'] : 1; ?>

<form method="post" action="">
    <?php for ($i = 0; $i < $num_inputs; $i++) : ?>
        <div><input type="text" name="inputs[]"/></div>
    <?php endfor ?>
</form>

Edit: yes, an array is much better than input_x. Updated my answer.

初心未许 2024-12-30 11:20:15

我认为你想要的是一系列表单字段。

您想要这样的内容:

<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter

echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);

?>

这将打印五个文本框:

<input type="text" name="mybox[]" />

然后,当您引用这些框的值时,您可以这样做:

<?php
    foreach ($_POST['mybox'] as $i) {
       echo $i;
    }

?>

也就是说,通过使用“mybox[]”作为每个输入字段的名称,您可以创建一个文本框数组,然后您可以对其进行迭代。

I think what you want is an array of form fields.

You want something like this:

<?php
$number_of_textboxes = 5; // you'd get this from a $_GET parameter

echo str_repeat('<input type="text" name="mybox[]" />', $number_of_textboxes);

?>

This will print five text boxes:

<input type="text" name="mybox[]" />

Then, when you reference these boxes' values, you do so like thus:

<?php
    foreach ($_POST['mybox'] as $i) {
       echo $i;
    }

?>

That is, by using "mybox[]" as the name of each input field, you create an array of textboxes, which you can then iterate through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文