首页 / PHP / 如何检查PHP中是否选中了复选框?
如何检查PHP中是否选中了复选框?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何检查PHP中是否选中了复选框?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2774字,纯文字阅读大概需要4分钟。
内容图文
我试图检查是否已在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>
这段代码出了什么问题?任何帮助,将不胜感激.
解决方法:
if(!isset($_POST['tos']))
$this->errors[] = 'Please accept our Terms of Service.';`
这样做是说如果$_POST中没有’tos’,则抛出一个错误.
在将表单发送到服务器之前,您应该使用javascript检查此客户端.
你也可以……
<input type="checkbox" name="tos" value="accepted" checked>
和PHP:
if(isset($_POST['tos']) && $_POST['tos']==='accepted') echo 'All good';
else array_push($errors,'err code here'); //to add to $error array
更新
我不确定你是否使用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.如果未选中,您甚至可以将表格全部停止.顺便说一句,如果我没记错,复选框输入只会在检查时发送POST数据.
内容总结
以上是互联网集市为您收集整理的如何检查PHP中是否选中了复选框?全部内容,希望文章能够帮你解决如何检查PHP中是否选中了复选框?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。