Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用CodeIgniter表单验证库验证空复选框并通过AJAX显示消息?_Php_Ajax_Forms_Codeigniter_Validation - Fatal编程技术网

Php 如何使用CodeIgniter表单验证库验证空复选框并通过AJAX显示消息?

Php 如何使用CodeIgniter表单验证库验证空复选框并通过AJAX显示消息?,php,ajax,forms,codeigniter,validation,Php,Ajax,Forms,Codeigniter,Validation,使用CodeIgniter表单验证库未选中复选框时显示表单错误消息 当前,仅当选中复选框时,才会显示表单错误消息 以下是AJAX调用: $('#add_user').on('submit', function(e) { e.preventDefault(); $.ajax({ url : $(this).attr('action'), method : "post", data: $(this).serialize(),

使用CodeIgniter表单验证库未选中复选框时显示表单错误消息

当前,仅当选中复选框时,才会显示表单错误消息

以下是AJAX调用:

$('#add_user').on('submit', function(e) {
    e.preventDefault();

    $.ajax({
        url : $(this).attr('action'),
        method : "post",
        data: $(this).serialize(),
        dataType: "json",
        success: function(result) {
            if(result.success == true) {
                alert('Success');
            } else {
                $.each(result.errors, function(key, value) {
                    var error = $('#' + key);
                    error.closest('div.form-group')
                    .find('.error').remove();
                    error.after(value);
                });
            }
        }
    });
});
这是PHP控制器:

$this->form_validation->set_rules('agreement', 'Agreement', 'callback_agreement_checkbox');

if($this->form_validation->run()) {
            $data['success'] = true;
            $this->load->view('Success');
        } else {
            foreach($this->input->post() as $key => $value) {
                $data['errors'][$key] = form_error($key);
            }
            echo json_encode($data);
        }

public function agreement_checkbox() {
        if (empty($this->input->post('agreement'))) {
            return FALSE;
        } else {
            $error = 'Please accept TOS';
            $this->form_validation->set_message('agreement_checkbox', $error);
            return TRUE;
        }
    }
在未选中复选框的情况下提交表单时,不会显示错误消息(但应该显示)。仅当选中复选框且该复选框错误时,才会显示该复选框

编辑:

我做了一些修改,以支持混合不同的输入数据:

$this->form_validation->set_rules('first_name', 'First name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last name', 'trim|required');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required');
$this->form_validation->set_rules('agreement', 'Agreement', 'required');

if($this->form_validation->run()) {
            $data['success'] = true;
            $this->load->view('Success');
        } else {                
            foreach($this->input->post() as $key => $value) {
                $data['errors'][$key] = form_error($key);
            }
            if (empty($this->input->post('agreement'))) {
                $data['errors']['agreement'] = form_error('agreement', '<div id="agreement_error">', '</div>');
            }           
            echo json_encode($data);
        }
$this->form_validation->set_规则('first_name','first name','trim | required');
$this->form_validation->set_规则('last_name','last name','trim | required');
$this->form_validation->set_规则('email'、'E-mail'、'trim | required');
$this->form_validation->set_规则('agreement'、'agreement'、'required');
如果($this->form\u validation->run()){
$data['success']=true;
$this->load->view('Success');
}否则{
foreach($this->input->post()作为$key=>$value){
$data['errors'][$key]=表格错误($key);
}
if(空($this->input->post('agreement')){
$data['errors']['agreement']=表格错误('agreement','');
}           
echo json_编码($data);
}

Codeigniter的回调函数未定义
$this->input->post()
。我建议您使用
required
。就是这样

$this->form_validation->set_rules('agreement', 'Agreement', 'required');
如果该复选框在提交之前已选中,则将定义该复选框。然后也更改此代码

if($this->form_validation->run()) {
    if(! empty($this->input->post('agreement))){
          $data['success'] = "It was checked";
        }else{
          $data['error'] = validation_errors();
    }
    echo json_encode($data);
}
您的ajax:

        success: function(result) {
        if(result.success) {
            console.log(result.success);
        } else {
            console.log(result.error);
        }
    }

让我知道结果。

Codeigniter的回调函数未定义
$this->input->post()
。我建议您使用
required
。就是这样

$this->form_validation->set_rules('agreement', 'Agreement', 'required');
如果该复选框在提交之前已选中,则将定义该复选框。然后也更改此代码

if($this->form_validation->run()) {
    if(! empty($this->input->post('agreement))){
          $data['success'] = "It was checked";
        }else{
          $data['error'] = validation_errors();
    }
    echo json_encode($data);
}
您的ajax:

        success: function(result) {
        if(result.success) {
            console.log(result.success);
        } else {
            console.log(result.error);
        }
    }

让我知道结果。

使用此
$this->form_validation->set_rules('agreement'、'agreement'、'required')使用此
$this->form_validation->set_规则('agreement'、'agreement'、'required')好的,我已经对您的解决方案做了一些修改,但它可以工作。看看我编辑过的帖子。好的,我对你的解决方案做了一些修改,但效果不错。看看我编辑的帖子。