php checkbox 取值詳細(xì)說明
更新時(shí)間:2010年08月19日 21:45:23 作者:
php的checkbox取值方式跟其它語言有點(diǎn)不同,主要是因?yàn)閜hp中數(shù)組的合理使用。
設(shè)我們有一個(gè)html頁面,代碼如下:
<FORM method="post" action="checkTest.php">
<INPUT name="test[]" type="checkbox" value="1" />
<INPUT type="checkbox" name="test[]" value="2" />
<INPUT type="checkbox" name="test[]" value="3" />
<INPUT type="checkbox" name="test[]" value="4" />
<INPUT type="checkbox" name="test[]" value="5" />
<INPUT type="submit" name="Submit" value="Submit" />
</FORM>
注意上面input的name屬性,各個(gè)屬性內(nèi)容都一樣,而且都是test[],加上[]的原因在于讓test的內(nèi)容變成數(shù)組形式傳遞。
checkTest.php的代碼內(nèi)容如下:
<?php
echo implode(",",$_POST['test']);
?>
我們輸出內(nèi)容時(shí)只需要注意利用implode函數(shù)將數(shù)組內(nèi)容轉(zhuǎn)化為字符串即可。
注:該功能可在刪除多記錄等場合運(yùn)用。如Delete from tbl where ID in (implode(",",$_POST['test']))即可。
實(shí)例代碼:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
</head>
<body>
html復(fù)選框如果要以數(shù)據(jù)組形式發(fā)送給php腳本處理就必須以如checkbox[]這形式
<form id="form1" name="form1" method="post" action="">
<label>
<input type="checkbox" name="checkbox[]" value="1" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="2" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="www.dhdzp.com" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="jb51.net" />
</label>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</form>
</body>
</html>
<?
//判斷是否點(diǎn)擊提交
if( $_POST )
{
$array = $_POST['checkbox'];
print_r($array);
}
/*
結(jié)果:
Array
(
[0] => 1
[1] => 2
[2] => www.dhdzp.com
[3] => jb51.net
)
簡單的很多事情在做之前覺得復(fù)雜但做起來就很容易了,像這個(gè)復(fù)選框代碼就是這樣了。
*/
?>
復(fù)制代碼 代碼如下:
<FORM method="post" action="checkTest.php">
<INPUT name="test[]" type="checkbox" value="1" />
<INPUT type="checkbox" name="test[]" value="2" />
<INPUT type="checkbox" name="test[]" value="3" />
<INPUT type="checkbox" name="test[]" value="4" />
<INPUT type="checkbox" name="test[]" value="5" />
<INPUT type="submit" name="Submit" value="Submit" />
</FORM>
注意上面input的name屬性,各個(gè)屬性內(nèi)容都一樣,而且都是test[],加上[]的原因在于讓test的內(nèi)容變成數(shù)組形式傳遞。
checkTest.php的代碼內(nèi)容如下:
復(fù)制代碼 代碼如下:
<?php
echo implode(",",$_POST['test']);
?>
我們輸出內(nèi)容時(shí)只需要注意利用implode函數(shù)將數(shù)組內(nèi)容轉(zhuǎn)化為字符串即可。
注:該功能可在刪除多記錄等場合運(yùn)用。如Delete from tbl where ID in (implode(",",$_POST['test']))即可。
實(shí)例代碼:
復(fù)制代碼 代碼如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標(biāo)題文檔</title>
</head>
<body>
html復(fù)選框如果要以數(shù)據(jù)組形式發(fā)送給php腳本處理就必須以如checkbox[]這形式
<form id="form1" name="form1" method="post" action="">
<label>
<input type="checkbox" name="checkbox[]" value="1" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="2" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="www.dhdzp.com" />
</label>
<label>
<input type="checkbox" name="checkbox[]" value="jb51.net" />
</label>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</form>
</body>
</html>
<?
//判斷是否點(diǎn)擊提交
if( $_POST )
{
$array = $_POST['checkbox'];
print_r($array);
}
/*
結(jié)果:
Array
(
[0] => 1
[1] => 2
[2] => www.dhdzp.com
[3] => jb51.net
)
簡單的很多事情在做之前覺得復(fù)雜但做起來就很容易了,像這個(gè)復(fù)選框代碼就是這樣了。
*/
?>
您可能感興趣的文章:
- PHP簡單獲取多個(gè)checkbox值的方法
- php一次性刪除前臺checkbox多選內(nèi)容的方法
- js限制checkbox勾選的個(gè)數(shù)以及php獲取多個(gè)checkbbox的方法深入解析
- php select,radio和checkbox默認(rèn)選擇的實(shí)現(xiàn)方法
- php checkbox復(fù)選框值的獲取與checkbox默認(rèn)值輸出方法
- php 用checkbox一次性刪除多條記錄的方法
- PHP中在數(shù)據(jù)庫中保存Checkbox數(shù)據(jù)(1)
- PHP中在數(shù)據(jù)庫中保存Checkbox數(shù)據(jù)(2)
- PHP中CheckBox多選框上傳失敗的代碼寫法
相關(guān)文章
PHP向?yàn)g覽器輸出內(nèi)容的4個(gè)函數(shù)總結(jié)
這篇文章主要介紹了PHP向?yàn)g覽器輸出內(nèi)容的4個(gè)函數(shù)總結(jié),本文總結(jié)的就是print()、echo()、printf()、sprintf()這4個(gè)輸出函數(shù),需要的朋友可以參考下2014-11-11
php自定義函數(shù)call_user_func和call_user_func_array詳解
看UCenter的時(shí)候有一個(gè)函數(shù)call_user_func,百思不得其解,因?yàn)槲乙詾槭亲约憾x的函數(shù),結(jié)果到處都找不到,后來百度了一下才知道call_user_func是內(nèi)置函數(shù)2011-07-07
php實(shí)現(xiàn)點(diǎn)擊可刷新驗(yàn)證碼
這篇文章主要介紹了php如何實(shí)現(xiàn)點(diǎn)擊即可刷新驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11
詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法
這篇文章主要介紹了詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法,文中講到了添加和移除簡碼等的一些PHP函數(shù)的用法,需要的朋友可以參考下2015-12-12
PHP日期函數(shù)date格式化UNIX時(shí)間的方法
這篇文章主要介紹了PHP日期函數(shù)date格式化UNIX時(shí)間的方法,實(shí)例分析了php中date函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03
php采用file_get_contents代替使用curl實(shí)例
這篇文章主要介紹了php采用file_get_contents代替使用curl的方法,實(shí)例講述了file_get_contents模擬curl的post方法,對于服務(wù)器不支持curl的情況來說有一定的借鑒價(jià)值,需要的朋友可以參考下2014-11-11

