如何检查PHP中是否选中复选框? [英] How do I check if checkbox is checked in PHP?

查看:102
本文介绍了如何检查PHP中是否选中复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个复选框是否已在PHP中检查为了注册。但它现在不工作。这是我目前使用的代码,以检查是否已经检查。

  if(!isset($ _ POST [ 'tos']))
$ this-> errors [] ='请接受我们的服务条款。

这是HTML代码。

 < div class =checkbox> 
< label>
< input type =checkboxname =tosvalue =0>我同意< a href =#>服务条款< / a>和< a href =#>隐私权政策< / a>
< / label>
< / div>

完整表单代码:

 < form role =formmethod =POSTaction =<?php echo $ _SERVER ['PHP_SELF'];?>> 
< div class =form-group>

< div class =row>
< div class =col-sm-6>
< label for =firstname> Firstname< / label>
< input type =textclass =form-controlid =firstnamename =firstnameplaceholder =Firstname>
< / div>
< div class =col-sm-6>
< label for =surname>姓氏< / label>
< input type =textclass =form-controlid =surnamename =surnameplaceholder =Surname>
< / div>
< / div>
< / div>
< div class =form-group>
< label for =username>用户名< / label>
< input type =textclass =form-controlid =rusername =ruserplaceholder =Username>
< / div>
< div class =form-group>
< label for =email2>电子邮件地址< / label>
< input type =emailclass =form-controlid =remailname =remailplaceholder =输入电子邮件>
< / div>
< div class =form-group>
< div class =row>
< div class =col-sm-6>
< label for =password2>密码< / label>
< input type =passwordclass =form-controlid =rpassname =rpassplaceholder =密码>
< / div>
< div class =col-sm-6>
< label for =password2>重复密码< / label>
< input type =passwordclass =form-controlid =rpass2name =rpass2placeholder =密码>
< / div>
< / div>
< / div>
< div class =checkbox>
< input type =checkboxname =tosvalue =0>我同意< a href =#>服务条款< / a>和< a href =#>隐私权政策< / a>
< / label>
< / div>
< input type =hiddenname =securvalue =<?php echo $ ip;?>/>
< input type =hiddenname =tokenvalue =<?php echo $ token;?>/>
< button type =submitname =registerclass =btn btn-block btn-color btn-xxl>创建帐户< / button>
< / form>

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

解决方案

  if(!isset($ _ POST ['tos ']))
$ this-> errors [] ='请接受我们的服务条款';`

这么做是说如果$ _POST中没有tos,就会抛出一个错误。



真的你应该检查这个客户端



您也可以...


$

b $ b

 < input type =checkboxname =tosvalue =accepted 

和PHP:

  if(isset($ _ POST ['tos'])&& $ _POST ['tos'] ==='accepted')echo'All good'; 
else array_push($ errors,'err code here'); //添加到$ error数组

更新 b
$ b

我不知道你是不是使用jQuery,但是只是为了更方便用户的方法:

  var tos = $('#tos'); 
var notice = $('。notice');
tos.on('click',function(){
if(tos.is(':checked')){
notice.text('Thank you。');
}
else {
notice.text('请接受我们的ToS继续。');
}
})

查看此处的工作示例。如果没有选中,你甚至可以一起停止表单。 BTW,如果我记得正确复选框输入只有在选中时才发送POST数据。


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.

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

And this is the HTML code.

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

解决方案

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.

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

You could also...

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

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

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 checkbox inputs only send POST data if checked.

这篇关于如何检查PHP中是否选中复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆