I have to process a Simple log-in File. In Many Web Tutorials I have read that for any Ajax requests in jquery the callback function is function(data) and the data is returned by the server side script.
Well, my server side script is PHP. I wish to know how can I return data from PHP which will be stored in jquery's data and I can use conditional loops to process them.
Here is my jquery Code:
$('#loginform').submit( function() {
var querystring = $(this).serialize();
$.post('login.php', querystring, processLI );
function processLI(data) {
if (data == 'success'){
alert("Successful");
var url = "game.php";
$(location).attr('href',url);
}
else
alert ('Login Failed');
}
I am using simple return statement in my php file, which does not seem to work at all. here is the login.php file. I just posted the part necessary here.
$statement = $connection->prepare("SELECT * FROM users WHERE username = '$username'");
$statement->execute(array());
$result = $statement->fetch(PDO::FETCH_ASSOC);
if ($result['password'] == $safepass) {
setcookie("Login", true);
echo 'success';
}
else
echo "Failure";
-
And where is the PHP code ?adeneo– adeneo2012年12月29日 07:50:51 +00:00Commented Dec 29, 2012 at 7:50
-
I have added the PHP code as well, now.Rohitink– Rohitink2012年12月29日 07:57:15 +00:00Commented Dec 29, 2012 at 7:57
-
And does it alert the error message (you did of course close the function, it's not closed in the code above) ?adeneo– adeneo2012年12月29日 07:58:34 +00:00Commented Dec 29, 2012 at 7:58
-
No Error Messages. I checked iin Chrome's JS Console as well. No error there too.Rohitink– Rohitink2012年12月29日 08:00:13 +00:00Commented Dec 29, 2012 at 8:00
-
1@RohitSmith try console.log(data); to check what is in response. if response is success that is fine .Surinder ツ– Surinder ツ2012年12月29日 08:21:53 +00:00Commented Dec 29, 2012 at 8:21
5 Answers 5
Try doing it like this, by placing the function as the parameter, and not by calling the function.
$('#loginform').submit( function() {
var querystring = $(this).serialize();
$.post('login.php', querystring, function(data){
if (data == 'success') {
alert("Successful");
var url = "game.php";
$(location).attr('href',url);
}
else
alert ('Login Failed');
});
2 Comments
This is an answer about how to debug AJAX requests. First, use Chrome (or Safari, or Firefox with Firebug plugin installed), then open up the developer tools from the settings menu. In the network panel, you can see the request/response. It may not be a direct answer, but please - try to use the Chrome developer tools with the "Net Panel" to see request/response/cookies/headers.
This will save you the trouble of having to guess, it will show you the response verbatim. Then you can solve it next time ;) and the time after
Have you been able to see the request/response? If not, I suggest a simple
alert(JSON.stringify(data))
...from your callback function if you have issues using the Chrome debugger.
Comments
Try giving the dataType for post as 'html'
Comments
$('#loginform').submit( function() {
var querystring = $(this).serialize();
$.ajax({
url : 'login.php?'+querystring,
cache : false,
success : function(data) {
if(data == "success") {
alert("Successful");
var url = "game.php";
$(location).attr('href',url);
} else if(data == "failure") {
alert("Login Failed");
}
};
});
});