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

如何在PHP中检查复选框是否已选中?,php,checkbox,Php,Checkbox,我试图检查是否在PHP中选中了一个复选框以便注册。但它现在不起作用。这是我目前正在使用的代码,用于检查是否已检查 if(!isset($_POST['tos'])) $this->errors[] = 'Please accept our Terms of Service.'; 这是HTML代码 <div class="checkbox"> <label> <input type="checkbox" name="tos" value="0"> I

我试图检查是否在PHP中选中了一个复选框以便注册。但它现在不起作用。这是我目前正在使用的代码,用于检查是否已检查

if(!isset($_POST['tos']))
  $this->errors[] = 'Please accept our Terms of Service.';
这是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>

我同意和
完整格式代码:

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

这意味着如果$\u POST中没有“tos”,则抛出一个错误

实际上,在表单发送到服务器之前,您可能应该使用javascript检查这个客户端

你也可以

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

我不确定您是否在使用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.');
  }
})

如果窗体未选中,您甚至可以一起停止窗体。顺便说一句,如果我没记错的话,
checkbox
inputs只在选中的情况下发送帖子数据。

你有HTML的帖子表单吗?变量转储($\u POST);是的,我有一个html代码的发布表单。你能发布完整的表单吗?进行一些调试,试着“打印”你的$发布,看看它有什么…现在发布完整的表单代码。我已经将值设置为0。我尝试了代码bro,但我不确定它是否有效。如果没有检查,我想将文本输入错误数组。尝试此-->如果(设置($\u POST['tos'])&&&$\u POST['tos']==1)$this->errors[]='All good';else$this->errors[]=“ToS未被接受…”;我不确定你有什么设置,但我认为你对阵列采取了JS方法。如果已经存在
$errors=array()
,则可以使用
array\u push($errors,'err code here')添加值。甚至
$errors[0]=“err code”,假设0为空,您不想每次都覆盖它。不过,说真的,如果您要通过POST传递输入,我会在使用javascript发送之前进行验证。我认为这将更加方便用户,而不是点击提交,必须返回,并可能丢失他们输入的数据。1)您仍然需要使用PHP进行检查。2) 添加
checked
是一个糟糕的建议,我从未见过一个网站这样做。3)
$\u POST['tos']==1
不起作用,输入参数是字符串。@KarolyHorvath关于它作为字符串传递的一点很好。我在OP中看到0,立即想到布尔值。我见过很多网站都有预先检查过的ToS。它为用户节省了一个步骤,用户可以清楚地看到这一步,他们无论如何都必须接受这一步。你仍然需要检查PHP——它是否存在。我还是说先验证客户端,这只是一个很好的实践。
if(isset($_POST['tos']) && $_POST['tos']==='accepted') echo 'All good'; 
else array_push($errors,'err code here'); //to add to $error array
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.');
  }
})