在php中处理一个表单的多个输入 [英] Handle multiple inputs of a form in php
问题描述
我想知道如何处理来自具有多个属性的表单的多个输入。
此代码生成我的字段:
< form method =POSTaction =test5.phpid = 1 >
<?
if($ _ SESSION [peoplecount]!= 0){
for($ i = 0; $ i< = $ _SESSION [peoplecount]; $ i ++){
echo'Name< input type =textname ='。$ i。'>成人< input type =radioname ='。$ i。'value =adult/>次要< input type =radioname ='。$ i。'value =minor/> <峰; br />;
}}
?>
< input class =buttontype =submitvalue =I / We Agreestyle =width:200px;/>
< / form>
一旦提交 - 或test5.php在action部分中提及,
foreach($ _POST as $ key => $ value){
print{$ key}:{$ value} < br />;
}
并且输出是
0:成人
1:成人
2:成人
3:成人
注意它有0,1,2 ......然后是成人。它甚至没有提到来自文本输入的人的名字。
我可以将表单改为:
< / blockquote>
< form method =POSTaction =test5.phpid =1>
<?
if($ _ SESSION [peoplecount]!= 0){
for($ i = 0; $ i< = $ _SESSION [peoplecount]; $ i ++){
echo'Name< input type =textname =usersnameid =usersname>成人< input type =radioname =agevalue =adultid =age/>次要< input type =radioname =agevalue =minorid =age/> <峰; br />;
}}
?>
< input class =buttontype =submitvalue =I / We Agreestyle =width:200px;/>
< / form>
使用相同的test5.php,我可以得到
用户名:
年龄:成人
年龄的值不会发布,并且test5.php中的foreach循环结束,因此线返回,直到它完全贯穿一个完整的帖子。
我希望我做了足够好的工作解释。
我希望我的输出是:
SomeName成人
SomeOtherName小
....
你的问题是你正在创建两个 $ i
的字符串来建立名称属性:
for($ i = 0; $ i< = $ _SESSION [peoplecount]; $ i ++){
echo'Name< input type =textname =name-'。 $ I'>。成人< input type =radioname =age - '。$ i。'value =adult/>次要< input type =radioname ='。$ i。'value =minor/> <峰; br />;
$ / code>
现在您的 $ _ POST
数组看起来像:
名字-0:somename age-0:成人
名字-1:其他名字年龄-1:小
...
更好的处理方式是使用数组作为表单名称属性,使用 []
(注意,为了避免所有额外的连接和复杂的引用,我在这里切换到了双引号。)
for($ i = 0; $ i <= $ _SESSION [peoplecount]; $ i ++){
echoName< input type ='text'name ='name [$ i]'> Adult< input type ='radio'name ='age [$ i]'value ='adult'/> Minor< input type ='radio' name ='age [$ i]'value ='minor'/>< br />;
$ / code>
在这种情况下,您的 $ _ POST
看起来像:
name:Array(
0:somename,
1:othername
),
age:Array(
0:adult,
1:minor
)
要访问它们,您可以像这样使用 foreach
循环:
foreach($ _POST ['name'] as $ key => $ name){
echo名称:$ name年龄:{$ _POST ['age ] [$关键]};
}
I want to know how to handle multiple inputs from a form with multiple atributes. This code generates my fields:
<form method="POST" action="test5.php" id="1">
<?
if($_SESSION["peoplecount"] != 0){
for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
echo ' Name<input type="text" name="'.$i.'"> Adult<input type="radio" name="'.$i.'" value="adult" /> Minor<input type="radio" name="'.$i.'" value="minor" /> <br/>';
} }
?>
<input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
</form>
once submitted -- or test5.php as its mentioned in the "action" part,
foreach ($_POST as $key => $value) {
print "{$key}: {$value}<br />";
}
and the output is
0: adult
1: adult
2: adult
3: adult
Notice it has 0, 1, 2... and then adult. It does not even mention the name of the person from the text input. I can alter the form to:
</blockquote>
<form method="POST" action="test5.php" id="1">
<?
if($_SESSION["peoplecount"] != 0){
for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
echo ' Name<input type="text" name="usersname" id="usersname"> Adult<input type="radio" name="age" value="adult" id="age"/> Minor<input type="radio" name="age" value="minor" id="age"/> <br/>';
} }
?>
<input class="button" type="submit" value="I/We Agree" style="width:200px;"/>
</form>
Using the same test5.php, I get
usersname:
age: adult
The value of the age is not posted and the the foreach loop in test5.php ends, hence the line return, before it completly runs through one complete post.
I hope I did a good enough job explaining. I want my output to be:
SomeName Adult
SomeOtherName Minor ....
Your problem is that you are creating two form inputs with name='$i'
, and the second one (the radio button) is overwriting the first. I would suggest instead that you use a string including $i
to build the name attributes:
for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
echo ' Name<input type="text" name="name-'.$i.'"> Adult<input type="radio" name="age-'.$i.'" value="adult" /> Minor<input type="radio" name="'.$i.'" value="minor" /> <br/>';
}
Now your $_POST
array will look like:
name-0: somename age-0: Adult
name-1: othername age-1: Minor
...
An even better way to handle it is to use arrays as form name attributes with []
(Note I've switched to double-quotes here, to avoid all the extra concatenation and complicated quoting.)
for ($i = 0; $i <= $_SESSION["peoplecount"]; $i++) {
echo " Name<input type='text' name='name[$i]'> Adult<input type='radio' name='age[$i]' value='adult' /> Minor<input type='radio' name='age[$i]' value='minor' /> <br/>";
}
In this case, your $_POST
looks like:
name: Array(
0: somename,
1: othername
),
age: Array (
0: adult,
1: minor
)
To access them, you can use a foreach
loop like so:
foreach ($_POST['name'] as $key=>$name) {
echo "Name: $name Age: {$_POST['age'][$key]}";
}
这篇关于在php中处理一个表单的多个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!