在網(wǎng)頁(yè)中,使用多選框可以一次性選擇多個(gè)屬性值,進(jìn)行批量操作。
網(wǎng)頁(yè)中checkbox的用法一般為:
<form name="formcode1" method="post" action=""><input name=code1 type=checkbox value=1><input name=code1 type=checkbox value=2><input name=code1 type=checkbox value=3><input type="submit" name="s" value="【批量刪除】"></form>
在asp中,可以直接使用 request.form("code1") 獲取參數值,request.form("code1")的值是以逗號 “, ”分割,比如上面如果全部勾選,那么獲得的值為:1, 2, 3。
可以使用split函數進(jìn)行切割,從而對每個(gè)參數進(jìn)行合適的處理。
在php編程中,是無(wú)法直接通過(guò)$_POST["code1"]獲取的,php中只能獲取最后一個(gè)值,比如全部勾選,$_POST["code1"]的值只返回3。
此時(shí),需要把表單 checkbox 的名稱(chēng)改為數組名:
<form name="formcode1" method="post" action=""><input name=code1[] type=checkbox value=1><input name=code1[] type=checkbox value=2><input name=code1[] type=checkbox value=3><input type="submit" name="s" value="【批量刪除】"></form>
因為,對于復選框checkbox值的獲取,php是作為數組對待的。
接下來(lái),就可以通過(guò) $_POST["code1"] 獲取數組array(1,2,3)里的全部值了。
再使用foreach函數,可以對數組array里的每個(gè)值進(jìn)行操作。
參考代碼:
<?php//獲取checkbox中的值$id=$_POST["code1"];foreach($id as $n){ //foreach循環(huán)遍歷 mysql_query("delete from content where id=$n",$conn); echo "·結果:刪除ID:(".$n.") 成功!<br />";}//edit by www.jbxue.com//at 2013/7/4?>
聯(lián)系客服