通过数组传递表单数据,可以保存数据之间的业务属性关系,比如有很多Student,每隔Student都有姓名、年龄、性别、爱好等表单信息。提交表单后还需要针对每个student进行处理或者保存。这样肯定需要为每个student的这些属性表单建立起关联关系,一种方式是根据属性表单的name上加特殊标记进行识别,但是数组传递表单就能使表单数据更结构化。
例子如下:
1
2
3
4
|
< input type = "hidden" name = "msginfo[name][]" value = "张三" />
< input type = "hidden" name = "msginfo[phonenum][]" value = "111111111" />
< input type = "hidden" name = "msginfo[name][]" value = "李四" />
< input type = "hidden" name = "msginfo[phonenum][]" value = "222222222" />
|
php代码:
1
2
3
4
|
<?php
$msgInfos = $_POST [ 'msginfo' ];
$phoneNums = $msgInfos [ 'name' ]; // 为array(-=>张三,1=>李四)
$phoneNums = $msgInfos [ 'phonenum' ]; // 为array(0=>111111111,1=>222222222)
|
例一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
if (isset( $_POST [ 'submit' ])){
$users = $_POST [ 'user' ];
foreach ( $users as $key => $val ){
echo 'user ' , $key , ' = ' , $val , '<br />' ;
}
}
?>
<form method= "post" >
zhangsan <input type= "text" name= "user[zhangsan]" value= "0" /><br />
lisi <input type= "text" name= "user[lisi]" value= "1" /><br />
wangwu <input type= "text" name= "user[wangwu]" value= "2" /><br />
zhaoliu <input type= "text" name= "user[zhaoliu]" value= "3" /><br />
<input type= "submit" name= "submit" value= "提交" />
</form>
|
例二
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<form method= "post" >
<?
for ( $i =0; $i <10; $i ++){
?>
<input type= "checkbox" name= "interests[]" value= "<?=$i?>" >test<?= $i ?><br>
<?
}
?>
<input type= "submit" >
</form>
<?php
<code class = "php keyword" > if (isset( $_POST )){
foreach ( $_POST as $key => $val ){
if ( is_array ( $val )){
foreach ( $val as $v2 ){
echo "$v2<br>" ;
}
}
}
}
?>
</code>
|
以上这篇form表单传递数组数据、php脚本接收的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。