[英]Form: make a loop to save different value of a same field name
I do a form, inside I would like to do a loop for to show the same field. 我做一个表格,我想做一个循环来显示相同的字段。 Each field will have different value and I would like to use sessions to take all the value. 每个字段将具有不同的值,我想使用会话来获取所有值。
Here is my code: 这是我的代码:
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "Numero ";
echo $i;
?>
<input type="text" name="number2" id="number2"/>
<?php
}
?>
</form>
<?php
echo $_POST['number2'];
$my_array=array($_POST['number2']);
$_SESSION['countnumb']=$my_array;
in another page: 在另一页中:
foreach($_SESSION['countnumb'] as $key=>$value)
{
echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
}
I can't register any number. 我无法注册任何号码。 How can I do this? 我怎样才能做到这一点? thanks 谢谢
Basics First - ids
should be unique in a webpage. 基础知识优先ids
在网页中应该是唯一的。
Declaring <input type="text" name="number2" id="number2"/>
in a loop is wrong . 在循环中声明<input type="text" name="number2" id="number2"/>
是错误的 。
For creating multiple input using loop try like this- 要使用循环创建多个输入,请尝试如下操作:
echo "<input type='text' name='number[$i]' id='number{$i}' />";
<input type="text" name="number[2]" id="number2"/>
would make $_POST['number']
an array which you can loop though server side, This is described here http://www.php.net/manual/en/faq.html.php#faq.html.arrays 会使$_POST['number']
成为一个可以在服务器端循环的数组,这在此处进行了描述http://www.php.net/manual/en/faq.html.php#faq.html.arrays
foreach ($_POST['number'] as $number){
echo $number;
}
for example 例如
this would make your code 这将使您的代码
for ($i = 1; $i <= 5; $i++) { //normally not 5 but a random number, choose by user
echo "<input type="text" name="number[$i]" id="number{$i}"/> ";
?>
<?php
}
?>
</form>
<?php
print_r( $_POST['number'] );
$_SESSION['countnumb']= $_POST['number'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.