1

I am trying to validate a registration form using codeigniter and ajax but I am getting empty response after I submit the form.

Here is the controller for the registration process.

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);   
        } 

Here is the ajax script

$("#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);
                    });
                }
            },
            });
      });
6
  • what is the controller name "account" or "Account" ? Commented Dec 14, 2016 at 7:07
  • @kashif its "Account" Commented Dec 14, 2016 at 7:08
  • But you are using url: "<?php echo base_url(); ?>account/individual_process", in ajax ( "account" instead "Account") Commented Dec 14, 2016 at 7:10
  • @kashif Does it matter? Commented Dec 14, 2016 at 7:12
  • No , it don't . but we need to follow the conventions . can you just share the error log Commented Dec 14, 2016 at 7:45

1 Answer 1

1

First Change it

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

To

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

Try with following script:

<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>
Sign up to request clarification or add additional context in comments.

2 Comments

@Sandeep Pariyar your are not sending d_o_b. Also send it from ajax call.
thanks bro, it worked !! I guess it was because of the missing d_o_b field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.