通過(guò)數組傳遞表單數據,可以保存數據之間的業務拍雨屬性關系,比如有很多Student,每隔Studen男厭t都(dōu)有姓名、年齡、性别、愛好(hǎo)等表單信息。提交表單市國後(hòu)還(hái)需要針對(duì)每個student進(舊舞jìn)行處理或者保存。這(zhè)樣請暗(yàng)肯定需要爲每個student的這(zhè)些屬性表單建立起(qǐ錢電)關聯關系,一種(zhǒng)方式是根據屬性表單的name上作黑加特殊标記進(jìn)行識别,但是數組傳遞表單就(jiù)能(néng)吃樹使表單數據更結構化。
例子如下:
<input type="hidden&q紙畫uot; name="msginfo[n老哥ame][]" value=&quo兵生t;張三"/>
<input type="hidden&q國她uot; name="msginfo[phone小吃num][]" value="1111照城11111"/>
<input type="hidden"照對; name="msginfo[name]答西[]" value="李四"/>
<input type="短姐hidden" name="msg光藍info[phonenum][]" value="冷花222222222"/>
php代碼:
<?php
$msgInfos = $_POST['msginf道現o'];
$phoneNums = $msgInfos['市間name']; // 爲array(-=>張子費三,1=>李四)
$phoneNums = $m弟山sgInfos['phonenum']; //愛民 爲array(0=>111111為秒111,1=>222222222)
例一
<?php
if(isset($_POST[湖畫39;submit'])){
$users = $_PO拿自ST['user'];
foreach($users a我廠s $key=>$val){說內
echo 長月9;user ',$key,件歌39; = ',$val,'<志少br />';
}
}
?>
<form method=明山"post">
zhangsan <input type="tex店費t" name=&quo樹照t;user[zhangsan]" value民章="0" 水秒/><br /&g村討t;
lisi <input type="te木也xt" name="土會;user[lisi]" v科月alue="1" />長票;<br />
wangwu <input type="text車北" name="user[wangwu]" 請鄉value="2" 明上/><br />
zhaoliu <inp嗎很ut type="text" name個醫="user[zhaoliu]" v師紅alue="3&quo開聽t; /><br />
<input type="sub下綠mit" name=&見笑quot;submit" value="提交" 愛暗/>
</form>
例二
<form method=視開"post">
<?
for($i=0;$i<1放一0;$i ){
?>
<input type="checkbox&綠又quot; name="interest腦亮s[]" value="<?=$i?>&quo術生t;>test<?=$i?><br如生>
<?
}
?>
<input type="submit&q司上uot;>
</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>";
}
}
}
}
?>
要保存很多表單信息,發(fā)現一個議鄉挺不錯的方法,以數組的形式把表單中得機的數據提交至後(hòu)台進(jìn)行保存。
<form id="form1" acti文開on="index1.html&quo遠地t; method="get&他快quot;> <div class="for金事m-control"> <畫舞;input type=&qu紙車ot;text" na你拿me="infos[1][name森老]" /> 生地 <input 麗作type="text" name="i短數nfos[1][num]" />車湖 <in視弟put type="text" name=&quo樹校t;infos[1][img]" /> <服木/div> <br> <們日div class="form-control&qu匠姐ot;> <input type=影東"text" name="infos[2]內東[name]" /&跳花gt; &l厭和t;input type="text"學分 name="inf水關os[2][num]" /> 兵會 <input ty我議pe="text" na森藍me="infos[2]說懂[img]" /> <熱大;/div> <br&g照紅t; <div class=&quo男你t;form-control"> &l中這t;input type="房謝;text" name="inf他劇os[3][name]" /> 海些 <input type="text"音火; name="infos[3][num]" />靜電 <input type="te裡站xt" name="i城北nfos[3][img]" /> </d妹和iv> ...... <input typ舞有e="submit&qu路短ot; value="Submit件吃" /> </form>
<input type="text" name=&都員quot;infos[1][name]" /> 森麗 <input type="text&quo多跳t; name="infos[山區1][num]" /> <inp和村ut type="text"老員 name="infos體體[1][img]" />