在網(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") 獲取參數(shù)值,request.form("code1")的值是以逗號(hào) “, ”分割,比如上面如果全部勾選,那么獲得的值為:1, 2, 3。 在php編程中,是無(wú)法直接通過(guò)$_POST["code1"]獲取的,php中只能獲取最后一個(gè)值,比如全部勾選,$_POST["code1"]的值只返回3。 此時(shí),需要把表單 checkbox 的名稱改為數(shù)組名: <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> 因?yàn)?,?duì)于復(fù)選框checkbox值的獲取,php是作為數(shù)組對(duì)待的。 接下來(lái),就可以通過(guò) $_POST["code1"] 獲取數(shù)組array(1,2,3)里的全部值了。 參考代碼: <?php //獲取checkbox中的值 $id=$_POST["code1"]; foreach($id as $n){ //foreach循環(huán)遍歷 mysql_query("delete from content where id=$n",$conn); echo "·結(jié)果:刪除ID:(".$n.") 成功!<br />"; } //edit by www. //at 2013/7/4 ?> |
|