如何检查复选框是否在 PHP 中被选中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21368625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:05:02  来源:igfitidea点击:

How do I check if checkbox is checked in PHP?

phpcheckbox

提问by Anders

I am trying to check if a checkbox have been checked in PHP in order to register. But it's not working right now. This is the code I am using so far, to check if it have been checked.

我正在尝试检查是否已在 PHP 中选中复选框以进行注册。但它现在不起作用。这是我目前使用的代码,用于检查它是否已被检查。

if(!isset($_POST['tos']))
  $this->errors[] = 'Please accept our Terms of Service.';

And this is the HTML code.

这是 HTML 代码。

<div class="checkbox">
<label>
<input type="checkbox" name="tos" value="0"> I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
</label>
</div>

Full form code:

完整表格代码:

        <form role="form" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
              <div class="form-group">

              <div class="row">
                  <div class="col-sm-6">
                  <label for="firstname">Firstname</label>
                  <input type="text" class="form-control" id="firstname" name="firstname" placeholder="Firstname">
                  </div>
                  <div class="col-sm-6">
                  <label for="surname">Surname</label>
                  <input type="text" class="form-control" id="surname" name="surname" placeholder="Surname">
                  </div>
              </div>
              </div>
              <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="ruser" name="ruser" placeholder="Username">
              </div>
              <div class="form-group">
                <label for="email2">Email address</label>
                <input type="email" class="form-control" id="remail" name="remail" placeholder="Enter email">
              </div>
              <div class="form-group">
                <div class="row">
                  <div class="col-sm-6">
                  <label for="password2">Password</label>
                  <input type="password" class="form-control" id="rpass" name="rpass" placeholder="Password">
                  </div>
                  <div class="col-sm-6">
                  <label for="password2">Repeat password</label>
                  <input type="password" class="form-control" id="rpass2" name="rpass2" placeholder="Password">
                  </div>
                </div>
              </div>
              <div class="checkbox">
                <label>
                  <input type="checkbox" name="tos" value="0"> I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>
                </label>
              </div>
               <input type="hidden" name="secur" value="<?php echo $ip;?>"/>
               <input type="hidden" name="token" value="<?php echo $token;?>"/>
              <button type="submit" name="register" class="btn btn-block btn-color btn-xxl">Create an account</button>
            </form>

What's wrong with this code? Any help would be appreciated.

这段代码有什么问题?任何帮助,将不胜感激。

回答by Casey Dwayne

if(!isset($_POST['tos']))
  $this->errors[] = 'Please accept our Terms of Service.';`

What this is doing is saying if there is no 'tos' in $_POST, throw an error.

这样做是说如果 $_POST 中没有“tos”,则抛出错误。

Really you should probably check this client side with javascript, beforethe form is sent to the server.

实际上,将表单发送到服务器之前,您应该使用 javascript 检查此客户端。

You could also...

你也可以...

<input type="checkbox" name="tos" value="accepted" checked>

And PHP:

和 PHP:

if(isset($_POST['tos']) && $_POST['tos']==='accepted') echo 'All good'; 
else array_push($errors,'err code here'); //to add to $error array

Update

更新

I'm not sure if you're using jQuery or not, but just for a more user friendly approach:

我不确定您是否使用 jQuery,但只是为了更用户友好的方法:

var tos = $('#tos');
var notice = $('.notice');
tos.on('click',function(){
  if(tos.is(':checked')){
    notice.text('Thank you.');
  }
  else{
   notice.text('Please accept our ToS to continue.');
  }
})

See a working example here.You could even halt the form all together if it's unchecked. BTW, if I remember correctly checkboxinputs only send POST data ifchecked.

请参阅此处的工作示例。如果未选中,您甚至可以一起停止表单。顺便说一句,如果我没记错的话,checkbox输入只有选中时才发送 POST 数据。