Rohitink
Rohitink

Reputation: 1174

How To Return Data from PHP to Use in jQuery(ajax)?

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";

Upvotes: 0

Views: 1864

Answers (5)

Get Off My Lawn
Get Off My Lawn

Reputation: 36299

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');
    });

Upvotes: 2

Talha Akbar
Talha Akbar

Reputation: 10030

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

Upvotes: 0

chwagssd
chwagssd

Reputation: 181

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.

Upvotes: 1

Chaitanya Munipalle
Chaitanya Munipalle

Reputation: 724

Try giving the dataType for post as 'html'

Upvotes: 0

Musa
Musa

Reputation: 97672

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

Upvotes: 1

Related Questions