JQuery - Accessing PHP array values ​​after AJAX POST

I want to access a PHP array using JavaScript after a successful POST.

PHP code:

return array('success' => true);

Javascript Code

$('#Get-Info').submit(function() {
$.post("info.php",
    function(data){
        if ( data['success'] ) {
            // Do things.
        }
    }
);
return false; });

The javascript function definitely works, it just cannot access the PHP array.

+3
source share
1 answer

Make php return json. Not sure about this part since I'm not a php programmer, but javascript will look like this:

$('#Get-Info').submit(function() {
$.post("info.php",
    function(data){
        if ( data['success'] ) {
            // Do things.
        }
    }, "json"
);
return false; });

The only difference is that jQuery will automatically parse the data as json, the datatype parameter. Additional information .

If I'm not mistaken, this should work for php, although it requires PHP 5.2.0:

echo json_encode(array('success' => true));

.

+3

Source: https://habr.com/ru/post/1779304/


All Articles