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

How do I check if checkbox is checked in PHP?(如何检查复选框是否在 PHP 中被选中?)
本文介绍了如何检查复选框是否在 PHP 中被选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

这是 HTML 代码.

<标签><input type="checkbox" name="tos" value="0">我同意<a href="#">服务条款</a>和<a href="#">隐私政策</a>

完整表格代码:

 <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 class="col-sm-6"><label for="surname">Surname</label><input type="text" class="form-control" id="surname" name="surname" placeholder="Surname">

<div class="form-group"><label for="username">用户名</label><input type="text" class="form-control" id="ruser" name="ruser" placeholder="用户名">

<div class="form-group"><label for="email2">电子邮件地址</label><input type="email" class="form-control" id="remail" name="remail" placeholder="输入电子邮件">

<div class="form-group"><div class="row"><div class="col-sm-6"><label for="password2">密码</label><input type="password" class="form-control" id="rpass" name="rpass" placeholder="密码">

<div class="col-sm-6"><label for="password2">重复密码</label><input type="password" class="form-control" id="rpass2" name="rpass2" placeholder="密码">

<div class="checkbox"><标签><input type="checkbox" name="tos" value="0">我同意<a href="#">服务条款</a>和<a href="#">隐私政策</a>

<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">创建账户</button></表单>

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

解决方案

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

这是说如果 $_POST 中没有tos",则抛出错误.

你真的应该用javascript检查这个客户端,表单被发送到服务器之前.

你也可以...

和 PHP:

if(isset($_POST['tos']) && $_POST['tos']==='accepted') echo 'All good';else array_push($errors,'此处的错误代码');//添加到$error数组

更新

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

var tos = $('#tos');var notice = $('.notice');tos.on('点击',function(){if(tos.is(':checked')){notice.text('谢谢你.');}别的{notice.text('请接受我们的 ToS 以继续.');}})

在此处查看工作示例. 如果未选中,您甚至可以一起暂停表单.顺便说一句,如果我没记错的话 checkbox 输入只发送 POST 数据 if 选中.

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 中被选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)