简体   繁体   English

当多个复选框具有相同形式时,如何通过POST通过PHP脚本访问复选框

[英]How to access a checkbox with a PHP script via POST when there are multiple checkboxes in the same form

I have a form for editing notifications that looks like: 我有一个用于编辑通知的表单,如下所示:

<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>

In edit.php , I want to set the value of $r_notif to 1 if checked ="yes" for the input with name resp_notif . edit.php ,如果将名称为resp_notif的输入checked ="yes" ,我想将$ r_notif的值设置为1 Similarly, I want to set the value of $c_notif to 1 if checked = "yes" for input c_notif . 类似地,如果输入c_notif checked = "yes" ,我想将$c_notif的值设置为1 I set them to zero otherwise in each case. 我将它们分别设置为零。

Problem is I only know how to access $_POST['name_of_field'] and don't know how to access the checked value...How can this be done (in PHP or otherwise) 问题是我只知道如何访问$_POST['name_of_field'] ,不知道如何访问检查的值...如何做到这一点(在PHP或其他方式中)

Thanks! 谢谢!

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

Try 尝试

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

and different variations of checking/not checking your checkboxes. 以及选中/不选中复选框的不同变体。 You should be able to answer your own question (literally, this is much encouraged on stackoverflow!) pretty soon. 您应该很快就能回答自己的问题(从字面上看,这在stackoverflow上是很受鼓励的!)。

It's a good idea to add value fields to your markup, that way you can be sure of what the contents of the POST will be, and it will not vary between browsers; 将值字段添加到标记中是个好主意,这样您就可以确定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" />

Then in your php; 然后在你的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM