0

I have a file ajax.php which I am using to process data passed through jquery. I have this particular line of code called on successful form verification:

$.post("/ajax.php",{'request': 'emailLogin', 'loginmail': mail, 'loginpass': pass}, function(data) {}  ); 

data in my case is: {"valid":true}{"auth":false}which is returned as a response from ajax.php, but I can't seem to file the correct way of defining "auth" and a variable with value "false".

My ajax.php is just checking if login and password are in the database and than echo json_encode(array('auth' => false)); or echo json_encode(array('auth' => true)); depending on the result. But it has also contain these lines:

if( isset($_POST['loginmail'])) {
    $usermail = htmlspecialchars($_POST['loginmail']);

    if (!filter_var($usermail, FILTER_VALIDATE_EMAIL)) {
        $response = array('valid' => false, 'message' => 'You did not enter a correct email address.'); 
    } else {                      
        // All good
        $response = array('valid' => true);
    }
}
echo json_encode($response);
12
  • Your data seems to be returning two JSON objects instead of one. Once you fix that, have you set the dataType to json in ajaxSetup? api.jquery.com/jQuery.ajaxSetup Commented Feb 6, 2015 at 1:00
  • You should post your ajax.php since the problem seems to be there and not in your JS Commented Feb 6, 2015 at 1:01
  • That is correct. One is used to confirm validation, another one authentication. Commented Feb 6, 2015 at 1:01
  • 1
    @IvanVenediktov You can only return one JSON object. You need to combine them into a single array, and then call json_encode() on that. Commented Feb 6, 2015 at 1:02
  • Can you please include your php code that generates the response data? Commented Feb 6, 2015 at 1:02

2 Answers 2

1

Don't echo json_encode($response) separately from the authentication result, you need to combine them. After you do the authentication, do:

$response['auth'] = $result_of_authentication;

then do

echo json_encode($response);

Once you do this, you should be able to access data.auth in Javascript. You should tell $.post that it's returning JSON:

$.post("/ajax.php",{
        'request': 'emailLogin', 
        'loginmail': mail, 
        'loginpass': pass}, 
    function(data) {
        alert(data.auth);
    },
    "json");
Sign up to request clarification or add additional context in comments.

1 Comment

I did that, good man, now my response is: {"valid":true,"auth":"false"}
0

Based on your PHP code you should be able to access the valid attribute like so:

$.post("/ajax.php",{'request': 'emailLogin', 'loginmail': mail, 'loginpass': pass}, function(data) {
    var auth = data.valid;
    if (auth) {
        // do something!
    } else {
        // do something else!
    }
}); 

Also there is a bug in your PHP code, you need to set up a default value for $response like so:

$response = array('valid' => false, 'message' => 'Email address is required');

if( isset($_POST['loginmail'])) {
    $usermail = htmlspecialchars($_POST['loginmail']);

    if (!filter_var($usermail, FILTER_VALIDATE_EMAIL)) {
        $response = array('valid' => false, 'message' => 'You did not enter a correct email address.'); 
    } else {                      
        // All good
        $response = array('valid' => true);
    }
}

echo json_encode($response);

Otherwise if $_POST['loginmail'] is not set your app with throw an undefined variable exception

EDIT:

As Barmar pointed out in his answer, you should only echo a response back once time to avoid creating an invalid response. Any data you need should be sent back in a single array. I don't see you doing that in your PHP code but you do make mention of echoing another array ['auth' => false] which will not work the way you want it to

4 Comments

This won't work until he fixes the problem with the invalid JSON.
I'm not sure from his code where invalid JSON would be coming from. Hopefully his example data was just incorrect
Oh I think I see what you are talking about, in his question he does mention echoing json_encodeded data twice...
@JonathanCrowe I did what Barmar said and tried your jquery code, but it doesn't seem to get data out of JSON.

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.