Thursday, July 4, 2019

PHP Codeigniter form submit using JQuery




I am new to PHP Codeigniter framework. I am designing a page in which I am using a link. On clicking the link it is calling a jquery function, which submits form through jquery. I have used codeigniter form validation methods for server side validation and for the timebeing I have disabled the client side validation.



The problem is that in this process when the form is submitted through jquery, the codeigniter form validation method is not working.



But if I am using a submit button to submit the form then the codeigniter form validation method works perfectly.



Please advise me what to do if I need to submit the form through jquery and use the codeigniter form validation method.



Please find the code below:




Login Form:





Login



Email:






Password:


















jquery function to submit the form on clicking the "link":





$("#login-submit").click(function()
{
$('#login-form').submit();

return false;
});



Controller function:





public function login_form()

{

$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$data['title'] = 'Log In';

$data['errorMessage'] = '';
$this->form_validation->set_rules('user-name', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');


if ($this->form_validation->run() == FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer');
}

else
{
$this->load->view('templates/header', $data);

$this->load->view('templates/menu');
$this->load->view('index', $data);
$this->load->view('templates/footer');
}

}



Here if I click on the "Submit button of the form, then the codeigniter validation works for user-name and password fields. But if I click the link with id="login-submit", then it calls the jquery function and the form get submitted. But the codeigniter validation does not work for user-name and password fields this time.




I need to submit the form through the link and the codeigniter validation function should work for this.



Thanks in Advance.....


Answer



Use .preventDefault() instead of just returning false on the anchor click event.



This has happened to me before and it seems to me that there is a conflict somewhere using .submit() inside a click event and returning false to stop the anchor's default behavior.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...