1

Up to this moment I was using this types of methods to send some variables via Ajax to server side php file and bring back some answer.

$('#some_form').ajaxSubmit({
    success: function(result)
    {
        some code....
    }
});


$.post('serverside_script.php', { variable: 'value' },
    function(result) 
    {
        some code...
    });

Answer was always in 1 variable and it was ok till now. But now I need several variables to come back from PHP side. How can I modify my scripts to get several variables back ?

1 Answer 1

6

The "result" in the callback you have showed is all that you could get from PHP - this is the server side response. You could retun JSON from PHP - something like this:

$json = json_encode(array('content' => 'some html content to show on page', 'var2' => 'value2', 'var3' => 'value3'));
echo $json;
exit;

Probably you will then need to parse the JSON: http://api.jquery.com/jQuery.parseJSON/

$.post('serverside_script.php', { variable: 'value' }, function(result) 
{
    result = jQuery.parseJSON(result);
    alert(result.content);
    alert(result.var2);
    alert(result.var3);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I edited my answer to show you the client-side usage of the JSON, as probably you will need that too.

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.