开发者

Getting multiple variables back from PHP file after ajaxSubmit or $.post method

开发者 https://www.devze.com 2023-04-05 18:46 出处:网络
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.

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....
    }
});开发者_StackOverflow社区


$.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 ?


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号