Welcome Guest, Not a member yet? Register   Sign In
Post values not send through with ajax and $this->input->post();
#1

[eluser]karimmaassen[/eluser]
In my home view, I created this form:

Code:
<div id="login-signup-form">
    &lt;?php
    echo form_open('login');
    echo form_input('username', 'Username', 'id="username"');
    echo form_password('password', 'Password', 'id="password"');
    echo form_submit('submit', 'Login', 'id="login_submit"');
    echo form_close();
    ?&gt;
</div>

With some basic javascript (thanks to Nettuts) I try to implement some ajax:

Code:
$('#login_submit').click(function() {

    var form_data = {
        username: $('#username').val(),
        password: $('#password').val()        
    };
    
    $.ajax({
        url: "/login",
        type: 'POST',
        data: form_data,
        success: function(msg) {
            $('#login-signup-form').html(msg);
        }
    });
    
    return false;
});

As you can see, it sends the form values to the login controller. The Login controller:

Code:
class Login extends CI_Controller {
    
    function index()
    {
        if($this->input->is_ajax_request())
        {
            echo '<h2>Login succeeded with ajax</h2>';
            echo '<p>Your username is '.$this->input->post('username').'</p>';
        }
        else
        {
            echo '<p>Your username is '.$this->input->post('username').'</p>';
            echo '<p>But your ajax failed miserably</p>';
        }
    }

}

The problem is, it doesn't work. The function `$this->input->is_ajax_request()` outputs FALSE. Ignoring that, `$this->input->post('username')` won't give me the username. It seems that post data isn't put through.


Is this a bug or a known issue?
#2

[eluser]InsiteFX[/eluser]
CodeIgniter CSRF Protection With Ajax - by Eric Barnes

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB