這里為大家?guī)硪黄狿HP實(shí)現(xiàn)form表單傳遞數(shù)組數(shù)據(jù)、php腳本接收的示例 。希望對(duì)您的學(xué)習(xí)研究PHP有幫助,具體參考描述如下:
通過數(shù)組傳遞表單數(shù)據(jù),可以保存數(shù)據(jù)之間的業(yè)務(wù)屬性關(guān)系,比如有很多Student,每隔Student都有姓名、年齡、性別、愛好等表單信息。提交表單后還需要針對(duì)每個(gè)student進(jìn)行處理或者保存。這樣肯定需要為每個(gè)student的這些屬性表單建立起關(guān)聯(lián)關(guān)系,一種方式是根據(jù)屬性表單的name上加特殊標(biāo)記進(jìn)行識(shí)別,但是數(shù)組傳遞表單就能使表單數(shù)據(jù)更結(jié)構(gòu)化。
例子如下:
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實(shí)現(xiàn)form表單傳遞數(shù)組數(shù)據(jù)、php腳本接收的示例 ,希望可以幫助到你。
延伸閱讀:
·Python 使用django form表單驗(yàn)證示例
·PHP解決CURL post數(shù)據(jù)報(bào)錯(cuò): failed creating formpost data
·php跨域提交form表單的實(shí)現(xiàn)方法
·Laravel使用FormRequest驗(yàn)證表單的實(shí)現(xiàn)方法
·php淺析number_format函數(shù)截取小數(shù)示例
·PHP來自FORM表單、URL參數(shù)的數(shù)據(jù)判斷是否是整數(shù)
·php提交form表單的實(shí)現(xiàn)方法
·php通過NumberFormatter格式化貨幣
·php+html5使用FormData對(duì)象提交表單、上傳圖片
·解決PHP提示Cannot modify header information - headers already sent by
|