[英]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.