A friend gave me the following piece of snippet:

json : { result: true }
success function(data){
    if(data.result) {
        //do something here
    }
    else {
        //do something here
    }
}

How would I integrate that code into the following code:

$(function() {
    $("#action_button").click(function() {
        var username = $("#username").val();
        var password = $("#password").val();
        var dataString = '&username=' + username + '&password=' + password;

        if (username=='' || password=='') {
            $('.success').fadeOut(200).hide();
            $('.error').fadeOut(200).show();
        }
        else {
            $.ajax({
                type: "POST",
                url: "processing/logsig.php",
                data: dataString,
                success: function(){
                    $('.success').fadeIn(200).show();
                    $('.error').fadeOut(200).hide();
                }
            });
        }
        return false;
    });
}

I'm returning JSON true or false values on the PHP side... Once the user inputs username and password, if the user is registered in the database, it logs him/her in. If the user isn't registered in the database, jQuery will load up a different form for the registration process.

share|improve this question

I hope you're sending the password via a secure connection... – animuson Oct 20 '11 at 16:11
1  
Hint: success: function(){. – Felix Kling Oct 20 '11 at 16:12
yeah I am... I'm using an HTTPs connection generated by PHP. – Michael Grigsby Oct 20 '11 at 16:18
feedback

2 Answers

up vote 1 down vote accepted

The success:function(data) is what your friend gave you. Your existing success:function() doesn't have the data parameter. That (data) is the JSON returned from your server/PHP script.

"success" indicates that the Ajax successfully returned data, but not whether or not the login was successful (username and password were correct). You should show your successful login if data.result == successful login, or whatever values your PHP JSON contains.

For code help, I'd need to see the returned JSON.

share|improve this answer
pastebin.com/g9htW2gM - the scripts too large to post in the comments, so there's the Pastebin link to my PHP code. – Michael Grigsby Oct 20 '11 at 16:28
I was really looking for the JSON rather than your PHP script. I can't see where you are returning the json to the Javascript, although I see where you are encoding it. I think your script will be something like if (data.session_state == true) // login success ... assuming you are doing a header('Content-type:application/json'); and then echo $json; – Matt H Oct 20 '11 at 16:34
Okay cool, so where is $json coming from? – Michael Grigsby Oct 20 '11 at 16:51
Yes, exactly my point: it must come from your php code, but it doesn't look like it is. You are doing an encoding, but nothing with the result : json_encode(array('session_state' => false)); .. The steps are: 1. Show form, users enters info and clicks submit and wait for result. 2. Receive form, check values, 2a. return result (json data). 3. Check result, show success or failure. You seem to be missing step 2a - your PHP script isn't returning the JSON result data to your page. – Matt H Oct 20 '11 at 16:58
$json = json_encode(array('session_state' => true)); echo $json; so do something like that? or do: return $json; ? – Michael Grigsby Oct 20 '11 at 17:06
show 3 more comments
feedback

It looks like the snippet would go in the 'success' function of your ajax call. You should also include the new parameter 'data' in the success function.

success: function(data){
    $('.success').fadeIn(200).show();
    $('.error').fadeOut(200).hide();
    if(data.result) {
    //do something here
    } 
    else {
    //do something here
    }
}
share|improve this answer
Yeah that's what I originally put the snippet in was an ajax call.. It seems like $.ajax syntax. – Michael Grigsby Oct 20 '11 at 16:30
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.