EDIT: Removing the ‘index.php’ with .htaccess creates this probem I just discovered. Now I’m on to resolving it.
EDIT: Problem solved. The javascript was wrong: url: "/login/" It needed a trailing slash.
ORIGINAL: In my home view, I created a form:
<div id="login"><?php
echo form_open('login');
echo form_input('username', 'Username', 'id="username"');
echo form_submit('submit', 'Login', 'id="login_submit"');
echo form_close();
?></div>
With some basic javascript (thanks to Nettuts) I tried to implement some ajax:
$('#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').html(msg);
}
});
return false;
});
As you can see, it sends the form values to the login controller.
class Login extends CI_Controller {
function index()
{
if($this->input->is_ajax_request())
{
echo '<h2>Login succeeded with ajax</h2>';
}
else
{
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, every other post data is missing. Nothing is put through.
What am I doing wrong?
I think your problem could be that you’re not actually sending to the correct script. Your url /login doesn’t look like it will get to a Codeigniter URL (http://domain.com/index.php/login or http://domain.com/my_app/index.php/login).
Try changing the url to either the full url location of the controller or to a proper absolute path. Either:
or
Unless you’ve rewritten the urls then /login probably won’t contact the correct script.