3
votes

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:

<?php echo validation_errors(); ?>
<form name="login-form" id="login-form" method="post" action="<?php echo base_url();?>index.php/login/login_form" >
    <H2>Login</H2>
    <div id="login-box-name">
        Email:
    </div>
    <div id="login-box-field">
        <input name="user-name" id="user-name" class="form-login" title="Please Enter Correct User Name" value="" size="30" maxlength="2048" />
    </div>
    <div id="login-box-name">
        Password:
    </div>
    <div id="login-box-field">
        <input name="password" id="password" type="password" class="form-login" title="Please Enter Correct Password" value="" size="30" maxlength="2048" />
    </div>
    <br />
    <span class="login-box-options">
        <input type="checkbox" name="1" value="1" title="Want this computer to remember you!!"> Remember Me <a href="#" id="login-div-change">Forgot password?</a>
    </span>
    <br />
    <br />
    <a href="" id="login-submit">
        <img src="<?php echo base_url();?>assets/images/login-btn.png" width="110" height="40" style="margin-left:90px;" />
    </a>
    <input type="submit" name="submit" id="submit" value="Submit" />
</form>

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.....

3
some code might help... Are you sure you are calling the same function in the controller for the jQuery and the default submit action?snaderss
DUDE! I totally love jquery and CI, use it all day long, and let me tell ya somthing that will help MAJORLY! Check out this jQuery Plugin it will make you life 100X easier, also remember to use print_r whenever you need to see something from the php side that isn't quite adding upSpYk3HH
Another tip I can give you bookmark this page and use it frequently, a great way to use it is to click the Table of Contents tab in upper left, when it opens, use (in chrome is what i use) ctrl+f to bring up the "find" then just type what you're looking for and find the link quick and easySpYk3HH
if you give me more to work with, (ie, show some code) I'll try and create you a full example from all 3 sides (jQuery, Form Plug, and CI)SpYk3HH
what do you mean its not working? have you checked in the network tab/firebug to see what the jquery function is returning?Rooster

3 Answers

3
votes

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.

2
votes

The code above is working fine. Actually I made a silly mistake in my code. I used following code for jQuery and it is working now.


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

Thanks

0
votes

it happens because it calls the function all the time when clicking on the button. you need to stop that.

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