0

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";
11
  • And where is the PHP code ? Commented Dec 29, 2012 at 7:50
  • I have added the PHP code as well, now. Commented 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) ? Commented Dec 29, 2012 at 7:58
  • No Error Messages. I checked iin Chrome's JS Console as well. No error there too. Commented 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 . Commented Dec 29, 2012 at 8:21

5 Answers 5

2

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');
    });
Sign up to request clarification or add additional context in comments.

2 Comments

No, this doesn't help either. No Matter what, Login Failed is echoed all the time. The problem is with returning value from PHP code.
parameter 2 of setcookie needs to be a string, not a boolean, you are probably getting an error returned back.
1

Use the echo statement to output data, if the login is successful echo 'success';

Comments

1

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

0

Try giving the dataType for post as 'html'

Comments

0
$('#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");
            }
        };
    });
});

4 Comments

this is GET method script so, in login.php use $_GET[index]
Sir, You changed the entire script. I asked for what was the fault in using the way i was doing. BTW, my main question was to how to return values from the php function..
well, i changed every thing because i can't understand your code and you said you need a callback variable then for your info in the success function the variable data contains all the data echoed in php file!
Just because you can't understand OP's code doesn't mean you should change everything. Instead, you should try to understand it and learn a thing or two in the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.