这里为大家带来一篇PHP实现form表单传递数组数据、php脚本接收的示例 。希望对您的学习研究PHP有帮助,具体参考描述如下:
通过数组传递表单数据,可以保存数据之间的业务属性关系,比如有很多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 | <form method= "post" >
<?
for ( $i =0; $i <10; $i ++){
?>
<input type= "checkbox" name= "interests[]" value= "<?=$i?>" >test<?= $i ?><br>
<?
}
?>
<input type= "submit" >
</form>
<?php
if (isset( $_POST )){
foreach ( $_POST as $key => $val ){
if ( is_array ( $val )){
foreach ( $val as $v2 ){
echo "$v2<br>" ;
}
}
}
}
?>
|
|
注:上面主要介绍了PHP实现form表单传递数组数据、php脚本接收的示例 ,希望可以帮助到你。
延伸阅读:
·Python 使用django form表单验证示例
·PHP解决CURL post数据报错: failed creating formpost data
·php跨域提交form表单的实现方法
·Laravel使用FormRequest验证表单的实现方法
·php浅析number_format函数截取小数示例
·PHP来自FORM表单、URL参数的数据判断是否是整数
·php提交form表单的实现方法
·php通过NumberFormatter格式化货币
·php+html5使用FormData对象提交表单、上传图片
·解决PHP提示Cannot modify header information - headers already sent by
|