I am creating an ajax login and it used to work but for some reason now it redirects me to my php login processing page. The code is below and I am using jquery ajax to submit my ajax request. The weird thing is that it apparently isn’t sending the variables because my page doesn’t echo the word ‘success’. Any help would be appreciated and also, would this be secure to use?
Javascript(jQuery);
$('.mainlogin').submit(function() {
var username = $('#main_username').val();
var password = $('#main_pword').val();
$.ajax({
url: 'log.php',
type: 'POST',
data: {
user: username,
pass: password
},
success: function(response) {
if (response == 'success') {
window.location.reload();
} else {
$('.logerror').fadeIn(250);
}
}
});
return false;
});
PHP;
<?php
require_once('.conf.php');
$user = mysql_real_escape_string($_POST['user']);
$pass = mysql_real_escape_string($_POST['pass']);
//$remem = mysql_real_escape_string($_POST['remem']);
//$expire=time() + 60 * 60 * 24 * 30;
$result = mysql_query("SELECT * FROM TABLENAME WHERE username = '$user'");
while ($row = mysql_fetch_array($result)) {
if (sha1($user.$pass) == $row['pword']) {
//setcookie('temp', $row['username']);
session_start();
$_SESSION['login'] = 1;
$_SESSION['uname'] = $row['username'];
$_SESSION['id'] = $row['id'];
echo "success";
}
}
?>
Thanks so much!! I can’t figure this out and have run multiple javascript checks, none of which gave me an error.
Is the login processing page the value of your form’s action attribute? If so it sounds like the form is being submitted instead of your AJAX being called.
Are you testing in a browser where you can see the response from your PHP script? Chrome/WebKit have a network tab where you can view the response of XHR (Ajax) requests.
You could try:
as your anonymous function definition to prevent the from from being submitted in the standard HTML fashion.