欢迎来到代码驿站!

PHP代码

当前位置:首页 > 软件编程 > PHP代码

form表单传递数组数据、php脚本接收的实例

时间:2021-03-02 11:44:18|栏目:PHP代码|点击:

通过数组传递表单数据,可以保存数据之间的业务属性关系,比如有很多Student,每隔Student都有姓名、年龄、性别、爱好等表单信息。提交表单后还需要针对每个student进行处理或者保存。这样肯定需要为每个student的这些属性表单建立起关联关系,一种方式是根据属性表单的name上加特殊标记进行识别,但是数组传递表单就能使表单数据更结构化。

例子如下:

<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代码:

<?php 
 $msgInfos = $_POST['msginfo'];
 $phoneNums = $msgInfos['name']; // 为array(-=>张三,1=>李四)
 $phoneNums = $msgInfos['phonenum']; // 为array(0=>111111111,1=>222222222)

例一

<?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>

例二

<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>

上一篇:详解yii2使用多个数据库的案例

栏    目:PHP代码

下一篇:IIS 7.5 asp Session超时时间设置方法

本文标题:form表单传递数组数据、php脚本接收的实例

本文地址:http://www.codeinn.net/misctech/72500.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有