Having a problem getting a form with ajax to get a php response. The javascript seems right because it works if I erase everything on login_ajax.php and only use the following
echo 'CORRECT'
//or
echo 'INCORRECT'
Once I use the real php code, the ajax doesn't get any response from the PHP. Even more weird if I remove the
return false
from the javascript and then submit the form, I do see CORRECT or INCORRECT being displayed on the login_ajax.php in the browser.
HTML:
<form id="login" action='login_ajax.php' method="get">
<label>Email: <input type="text" name="email" id="email" value="[email protected]"></label>
<label>Password: <input type="text" name="password" id="password" value="x"></label>
<input type="submit" value="login">
<div id="straight_response">php response here</div>
<div id="message">status</div>
</form>
PHP:
if (isset($_GET['password'])) {
$email = $_GET['email'];
$password = $_GET['password'];
$stored_pass = "123";
if ($password == $stored_pass) {
echo "CORRECT";
} else {
echo "INCORRECT";
}
}
//Javascript works when using one of the two lines below instead of the code above
//echo 'INCORRECT';
//echo 'CORRECT'
JAVASCRIPT:
$('#login').submit(function(){
var email;
var password;
email = $('#email').val();
password = $('#password').val();
var data = {};
data.email = email;
data.password = password;
var options = {};
options.dataType = 'text';
options.type = 'get';
options.url= 'login_ajax.php';
options.success = function (response) {
// jQuery.trim(response);
console.log(response.results);
console.log(response.query);
$('#always').text(response);
if (response === 'CORRECT') {
$('#message').text('you got it right');
console.log("good combination");
}
else if (response === 'INCORRECT') {
$('#message').text('sorry, try again');
console.log("bad combination");
}
};
$.ajax(options);
return false;
});//#login function