通过表单向php后台传多条数据,以关联数组方式呈现,废话不多说,代码附上:
html表单代码,方式我设置为get:
1 <form action="php/cart.php" method="GET"> 2 <label for="mmm">数据1</label> 3 <input type="text" name="cName[name1]" > 4 <br> 5 <label for="nnn">数据2</label> 6 <input type="text" name="cName[name2]"> 7 <br> 8 <input type="submit" value="提交给cartphp"> 9 </form>
php中的代码:
1 <?php 2 $arr=$_GET['cName']; 3 foreach($arr as $K => $V){ 4 echo $K."的值为:".$V."<br>"; 5 } 6 ?>
因为是关联数组,所以for循环这里就用不了了,foreach才是遍历关联数组的关键。
其中的$K,$V分别是自己定义的键和值。
运行效果如下: