简体   繁体   中英

Getting particular data from json_encode response

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM