2011-10-09 29 views
0

我對編輯的通知,看起來像一個形式:如何通過POST PHP腳本訪問複選框,當有相同的形式多個複選框

<form action='edit.php' method = 'post'> 
<b> Username</b>:<br/> <input type='text' name ='username' value ="<?=$username?>" /><br/> 

<input type = "checkbox" id="r_notif" name="r_notif" checked="yes" /> Response Notifications<br/> 
<input type = "checkbox" id="c_notif" name="c_notif" checked="yes" /> Comment Notifications<br/> 
<input type ='submit' value = 'SEND' /> 
</form> 

edit.php,我想設置的值$ r_notif爲1,如果checked ="yes"用於名稱爲resp_notif的輸入。同樣,如果輸入c_notif的輸入爲checked = "yes",我想將$c_notif的值設置爲1。在每種情況下,我都將它們設置爲零。

問題是,我只知道如何訪問$_POST['name_of_field'],不知道如何訪問檢查值...如何才能做到這一點(在PHP或其他方式)

謝謝!

回答

0

將值域添加到標記是一個好主意,這樣您就可以確定POST的內容是什麼,並且它在瀏覽器之間不會改變;

<input type = "checkbox" id="r_notif" name="r_notif" checked="yes" value="yes" /> 
<input type = "checkbox" id="c_notif" name="r_notif" checked="yes" value="yes" /> 

然後在你的php;

if($_POST['r_notif'] == 'yes') $r_notif = 1; 
else $r_notif = 0; 
if($_POST['c_notif'] == 'yes') $c_notif = 1; 
else $c_notif = 0; 
0
if(isset($_POST['name_of_field'])) 
0

嘗試的檢查/不檢查你的複選框

var_dump($_POST['r_notif']); 
var_dump($_POST['c_notif']); 

和不同的變化。你應該能夠很快回答你自己的問題(從字面上看,這在stackoverflow上是非常鼓舞人心的!)。