使用ajax进行Codeigniter表单验证会返回空白响应

时间:2016-12-14 06:57:48

标签: php jquery ajax codeigniter validation

我正在尝试使用codeigniter和ajax验证注册表单,但在提交表单后我得到空的响应。

这是注册过程的控制器。

public function individual_process(){
            $full_name = $this->input->post('full_name');
            $email_address = $this->input->post('email_address');
            $password = $this->input->post('password');
            $cpassword = $this->input->post('cpassword');
            $d_o_b = $this->input->post('d_o_b');
            $gender = $this->input->post('gender');

            // form validation rules
            $this->form_validation->set_rules('full_name', 'Full Name', 'trim|required');
            $this->form_validation->set_rules('email_address', 'Primary Email', 'trim|required');
            $this->form_validation->set_rules('password', 'Password', 'trim|required');
            $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
            $this->form_validation->set_rules('d_o_b', 'Date of Birth', 'trim|required');
            $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
            // $this->form_validation->set_rules('g-recaptcha-response','Captcha','callback_recaptcha');

            $this->form_validation->set_error_delimiters('<div class="error" style="color:red; margin-top:5px; ">', '</div>');

            if ($this->form_validation->run() == FALSE){
                    foreach($_POST as $key => $value){
                    $data['error'][$key] = form_error($key);
                }
            }else{
                $data['success'] = true;
            }   
            echo json_encode($data,true);   
        } 

这是ajax脚本

$("#step_submit").click(function(){
            var full_name = $("#full_name").val();
            var email_address = $("#email_address").val();
            var password = $("#password").val();
            var cpassword = $("#cpassword").val();
            var gender = $("#gender").val();    

            $.ajax({
            url: "<?php echo base_url(); ?>account/individual_process",
            type: "post",
            data: {full_name : full_name, email_address : email_address, password : password, cpassword : cpassword, gender : gender},
            dataType: 'json',
            async: false,
            success: function (response) {
                if(response.success == true){
                    location.reload();
                }else{
                    //alert('error');
                    $(".error").html(' ');
                    $.each(response.error, function(key, value){
                        var element = $('#' + key);
                        element.closest('div.form-group')
                        .removeClass('has-error')
                        .addClass('has-error') 
                        .find('.error').remove();
                        element.after(value);
                    });
                }
            },
            });
      });

1 个答案:

答案 0 :(得分:1)

首先改变它

data: {full_name : full_name, email_address : email_address, password : password, cpassword : cpassword, gender : gender},

data: {"full_name" : "full_name", "email_address" : email_address, "password" : password, "cpassword" : cpassword, "gender" : gender},

尝试使用以下脚本:

<script type="text/javascript">
    $("#step_submit").click(function(){
            var full_name = $("#full_name").val();
            var email_address = $("#email_address").val();
            var password = $("#password").val();
            var cpassword = $("#cpassword").val();
            var gender = $("#gender").val();    

            $.ajax({
            url: "<?php echo base_url('account/individual_process');?>",
            type: "post",
            data: {"full_name" : "full_name", "email_address" : email_address, "password" : password, "cpassword" : "cpassword", "gender" : gender},
            dataType: 'json',
            success: function (response) {
                var response=eval(response);
                if(response.success == true){
                    location.reload();
                }else{
                    //alert('error');
                    $(".error").html(' ');
                    $.each(response.error, function(key, value){
                        var element = $('#' + key);
                        element.closest('div.form-group')
                        .removeClass('has-error')
                        .addClass('has-error') 
                        .find('.error').remove();
                        element.after(value);
                    });
                }
            },
            });
      });
</script>