[英]Is this the correct way to check in PHP that a checkbox is checked?
我有以下代码来检查我的ToS复选框是否已选中:
if (!isset($_POST['tosagree'])) {//if the user did not agree to ToS
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
由于某种原因,它允许我在其中注册该代码。 奇怪的是,如果没有其他任何字段,并且未选中该字段,它将打印错误,但是,如果这是唯一的问题,则允许用户注册。
更新:似乎是我的代码中有些奇怪的问题。 如果您想遍历217行代码,请参考以下所有代码: http : //pastebin.com/YkERYpeF
!isset($_POST['tosagree'])
是否足够取决于浏览器。
为了安全起见,请始终设置复选框的value
属性。 然后,您只需检查该值是否存在:
<input type='checkbox' value='agreed'/>
然后执行以下操作:
if (!isset($_POST['tosagree']) || $_POST['tosagree'] != "agreed" ) {
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
另外,当未明确指定value
属性时,浏览器通常会On
选中该value
时将其设置为On
。
好吧,我知道发生了什么事。 在您的PHP代码中,您具有以下代码块:
if (!isset($_POST['tosagree']) || $_POST['tosagree'] != "agreed") {//if the user did not agree to ToS
$errors[] = '<span>Please agree to the Terms of Service.<span>';
}
然后,您有以下一行:
if ($un && $e && $p && $ic) { //if there are no errors
问题在于,当未选中“ TOS”复选框时,您没有标志。 无法打印错误,因为在if
语句中,您需要调用exit()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.