[英]checkbox in PHP validation
我一直在试图弄清楚这一点,并遇到了PHP新问题。
在将表单提交到数据库之前,我需要选中一个复选框(需要他们确认用户协议的注册表单)。
我不确定即时通讯会出错,即使在处理了该问题的其他几篇文章后,即时通讯也没有任何运气。
以下是适用的主要代码部分,任何可以提供一些指导的人将不胜感激。
(复选框的特定代码位于表单的每个部分的底部,是该表单的最新内容)
从验证
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
if($rule === 'required' && $rule_value === true && empty($value)) {
$this->addError("{$item} is required.");
} else if (!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$item}.");
}
break;
case 'unique':
$check = $this->_db->get('users', array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} is already in use.");
}
break;
case 'accepted':
if(!isset($_POST['useragreement'])){
$this->addError("You must agree to our terms and conditions");
}
break;
从实际表格处理
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'),
'password' => array(
'required' => true,
'min' => 6),
'password_again' => array(
'required' => true,
'matches' => 'password'),
'firstname' => array(
'required' => false,
'min' => 2,
'max' => 50),
'lastname' => array(
'required' => false,
'min' => 2,
'max' => 50),
'dob' => array(
'required' => false),
'gender' => array(
'required' => false),
'nationality' => array(
'required' => false),
'email' => array(
'required' => true,
'min' => 6,
'unique' => 'email'),
'email_again' => array(
'required' => true,
'matches' => 'email'),
'useragreement' => array(
'accepted' => true)
));
HTML
<div class="regfieldcheck">
<label for="useragreement"><span class="requiredfield">*</span> I agree to the User Agreement.</label>
<input id="useragreement" type="checkbox" />
</div>
您的HTML表单仅指定id="useragreement"
,仅用于javascript / css。
要在$_POST
数组中获得“用户同意”(以便您的函数可以看到它),您还需要指定“名称”。
尝试更改此...
<input id="useragreement" type="checkbox" />
为此...
<input id="useragreement" name="useragreement" type="checkbox" />
回到基础。 首先,我要为复选框指定一个名称...
<input id="useragreement" type="checkbox" name="useragreement"/>
在您发布表单的页面上,也只需插入表单数据
var_dump($_POST);
然后,您可以查看已过帐的数据。 您可以同时勾选和取消勾选复选框,然后查看不同的结果。
然后,您可以使用简单的IF
语句。 所以...
if(isset($_POST['useragreement'])){
if($_POST['useragreement']){
//If True (ticked)
}else{
//If Fale (unticked)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.