Welcome Guest, Not a member yet? Register   Sign In
Codeigniter form validation not working while using Ajax form submission ...
#1

[eluser]Kiran cet[/eluser]

This is my Ajax form submission code.
Code:
[removed]

  $(document).ready(function()
  {
   $('#submit').click(function()
   {
     var form_data =
     {
      college_code:   $('#college_code').val(),
      college_name:   $('#college_name').val(),
      college_district:  $('#college_district').val(),
      college_pincode:  $('#college_pincode').val(),
      college_city:   $('#college_city').val(),
      college_phone:   $('#college_phone').val(),
      college_email:   $('#college_email').val(),
      college_url:   $('#college_url').val(),
      admin_id:    $('#admin_id').val(),
      ajax:     '1'
     };

     $.ajax(
     {
      url: "<?php echo site_url('College_registration/registration'); ?>",
      type: 'POST',
      async : false,
      data: form_data,
      success: function(msg)
      {
       alert("a");
      }
     }  );
    
     return false;
   } );
  } );
  
[removed]

But while submission the codeigniter form validation not working ??
Please help me.

How can i fix it ?
#2

[eluser]rwestergren[/eluser]
The form validation code is server-side, so we'd need to see your "registration" function.

You may also want to look into the jQuery Form Plugin rather than manually getting the values of your form.
#3

[eluser]Kiran cet[/eluser]
This is my Controller

Code:
class College_registration extends CI_Controller
{
function __construct()
{
  parent::__construct();
  
}

function index()
{
  $this->template->current_view = ('college');
  $this->template->render();
}

function registration()
{
  
  $this->form_validation->set_error_delimiters('<div class="error2">', '</div>');
  $this->form_validation->set_rules('college_url','this field','required|prep_url');
  $this->form_validation->set_rules('admin_id','this field','required|is_unique[admin_details.userid]');
  $this->form_validation->set_rules('college_code','college code',
           'required|alpha|exact_length[3]|is_unique[college_details.code]');
  $this->form_validation->set_rules('college_name','this field','trim|required');
  $this->form_validation->set_rules('college_district','district name','required|alpha');
  $this->form_validation->set_rules('college_pincode','pincode','required|is_natural|exact_length[6]');
  $this->form_validation->set_rules('college_city','this field','required|alpha');
  $this->form_validation->set_rules('college_phone','phone','required|is_natural|min_length[10]');
  $this->form_validation->set_rules('college_email','email','required|valid_email');



   if($this->form_validation->run()==FALSE)
   {
  
    // oops error
   }
  
   else
   {
     $this->load->model('College_Register_Model');
     $this->College_Register_Model->insert_data(); //data inserted
   }
}
}


how can i use is_ajax_request() in my controller ?
how to use json_encode() ?
i am printing error message using form_error('field name') , is not printing while using ajax submission .
#4

[eluser]CroNiX[/eluser]
[quote author="rwestergren" date="1343234794"]
You may also want to look into the jQuery Form Plugin rather than manually getting the values of your form.[/quote]
jQuery has a built in function for that. $(form_id).serialize();
#5

[eluser]CroNiX[/eluser]
[quote author="Kiran cet" date="1343235606"]
how can i use is_ajax_request() in my controller ?
how to use json_encode() ?
i am printing error message using form_error('field name') , is not printing while using ajax submission .[/quote]

Code:
if ($this->input->is_ajax_request())

Code:
echo json_encode($array);

I'm not sure how you expect to use form_error(), which is PHP/CI, directly with javascript.

if there are form errors, use validation_errors() in your controller and send back the resulting HTML and display the errors in a div.

I usually do something like:

Code:
$data['status'] = $this->form_validation->run();  //did we pass validation?
if ($data['status'] === FALSE)
{ //failed
  $data['message'] = validation_errors(); //save the errors in html
}
else
{ //passed
  $data['message'] = 'The data was saved';
}

//send json result back to jquery
echo json_encode($data);  //send the array back to javascript using json

Then in the success event of your ajax call...something like:
Code:
success: function(data) {
  if (typeof(data.status) != 'undefined') {  //see if data.status exists
    if (data.status) {
     //passed validation, do something
    } else {
     //failed validation, display errors
     $('#my-error-display-div').html(data.message);
    }
  }
}
#6

[eluser]Kiran cet[/eluser]
Thanks for your support. I have almost done.

I have another doubt with how to access the form variable in my controller.. ??

Currently i am accessed it through
Code:
$this->input->post('college_code')
is this good to use when i am using ajax form submission ??
Thanks...
#7

[eluser]CroNiX[/eluser]
Yes.
#8

[eluser]kakallatt[/eluser]
[quote author="CroNiX" date="1343243633"][quote author="Kiran cet" date="1343235606"]
how can i use is_ajax_request() in my controller ?
how to use json_encode() ?
i am printing error message using form_error('field name') , is not printing while using ajax submission .[/quote]

Code:
if ($this->input->is_ajax_request())

Code:
echo json_encode($array);

I'm not sure how you expect to use form_error(), which is PHP/CI, directly with javascript.

if there are form errors, use validation_errors() in your controller and send back the resulting HTML and display the errors in a div.

I usually do something like:

Code:
$data['status'] = $this->form_validation->run();  //did we pass validation?
if ($data['status'] === FALSE)
{ //failed
  $data['message'] = validation_errors(); //save the errors in html
}
else
{ //passed
  $data['message'] = 'The data was saved';
}

//send json result back to jquery
echo json_encode($data);  //send the array back to javascript using json

Then in the success event of your ajax call...something like:
Code:
success: function(data) {
  if (typeof(data.status) != 'undefined') {  //see if data.status exists
    if (data.status) {
     //passed validation, do something
    } else {
     //failed validation, display errors
     $('#my-error-display-div').html(data.message);
    }
  }
}
[/quote]

Please help me.

This is my controller.
Code:
$this->form_validation->set_rules("title","Title","trim|required|max_length[128]|xss_clean");
                $this->form_validation->set_rules("link","URL website","trim|required|xss_clean");

                $data['status'] = $this->form_validation->run();
                if ($data['status'] === TRUE)
                    $title          = $this->input->post("title");
                    $link           = $this->input->post("url");
                    $catId          = $this->input->post("cat");
                    $website        = getWebFromUrl($link);
                    //$description    = $this->getInfoLink($link, "description");

                    $data = array(
                                    "title_web"     => $title,
                                    "link"          => $link,
                                    //"description"   => $description,
                                    "website"       => $website,
                                    "cat_id"        => $catId,
                                    "user_id"       => $this->auth->id,
                                    "post_date"     => date("Y-m-d H:i:s"),
                            );

                    if ($this->PookieModel->addPookie($data)) {
                        $data["message"] = "Success.";
                    }
                    else {
                        $data["message"] = "Have a problem.";
                    }
                }
                else {
                    $data['message'] = validation_errors();
                }

                echo json_encode($data);

Ajax:
Code:
[removed]
    $(document).ready(function () {
        $('#btn_addPookie').click(function () {
            var url = $('#aP_url').val();
            var title = $('#aP_title').val();
            var cat = $('#aP_cat').val();
            $.ajax({
                type: "POST",
                url: "&lt;?php echo base_url('pookie/add-pookie'); ?&gt;",
                data: {
                    url: url,
                    title: title,
                    cat: cat
                },
                success: function (data) {
                    if (typeof(data.status) != 'undefined') {  //see if data.status exists
                      if (data.status) {
                        $('#result').html(data);
                      } else {
                       //failed validation, display errors
                       $('#my-error-display-div').html(data.message);
                      }
                    }
                }
            });
            return false;
        });
    });
[removed]

Final, in my view has a div to show message.
Code:
<div id="my-error-display-div"></div>

But the form validation not working when I click submit button. Please help me. Thank you so much!!!!




Theme © iAndrew 2016 - Forum software by © MyBB