[英]How to access a checkbox with a PHP script via POST when there are multiple checkboxes in the same form
我有一个用于编辑通知的表单,如下所示:
<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
,如果将名称为resp_notif
的输入checked ="yes"
,我想将$ r_notif的值设置为1
。 类似地,如果输入c_notif
checked = "yes"
,我想将$c_notif
的值设置为1
。 我将它们分别设置为零。
问题是我只知道如何访问$_POST['name_of_field']
,不知道如何访问检查的值...如何做到这一点(在PHP
或其他方式中)
谢谢!
if(isset($_POST['name_of_field']))
尝试
var_dump($_POST['r_notif']);
var_dump($_POST['c_notif']);
以及选中/不选中复选框的不同变体。 您应该很快就能回答自己的问题(从字面上看,这在stackoverflow上是很受鼓励的!)。
将值字段添加到标记中是个好主意,这样您就可以确定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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.