0

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.

3
  • I hope you're sending the password via a secure connection... Commented Oct 20, 2011 at 16:11
  • 1
    Hint: success: function(){. Commented Oct 20, 2011 at 16:12
  • yeah I am... I'm using an HTTPs connection generated by PHP. Commented Oct 20, 2011 at 16:18

2 Answers 2

1

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.

7
  • pastebin.com/g9htW2gM - the scripts too large to post in the comments, so there's the Pastebin link to my PHP code. Commented Oct 20, 2011 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; Commented Oct 20, 2011 at 16:34
  • 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. Commented Oct 20, 2011 at 16:58
  • $json = json_encode(array('session_state' => true)); echo $json; so do something like that? or do: return $json; ? Commented Oct 20, 2011 at 17:06
  • Yes, something like that. Make sure you also set the content-type header as I noted above. Commented Oct 20, 2011 at 17:34
0

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
    }
}
1
  • Yeah that's what I originally put the snippet in was an ajax call.. It seems like $.ajax syntax. Commented Oct 20, 2011 at 16:30

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.