当同一表单中有多个复选框时,如何通过POST访问带有PHP脚本的复选框

时间:2011-10-09 17:09:05

标签: php javascript jquery html forms

我有一个用于编辑通知的表单,如下所示:

<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_notif1,我想将checked = "yes"的值设置为c_notif。在每种情况下,我将它们设置为零。

问题是我只知道如何访问$_POST['name_of_field']并且不知道如何访问选中的值...如何做到这一点(PHP或其他方式)

谢谢!

3 个答案:

答案 0 :(得分:0)

if(isset($_POST['name_of_field']))

答案 1 :(得分:0)

尝试

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

以及检查/不选中复选框的不同变体。你应该能够很快回答你自己的问题(字面意思,这在stackoverflow上非常鼓励!)。

答案 2 :(得分: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;