drgzmmy6379 2012-04-11 00:55
浏览 13
已采纳

如何将PHP *变量*发送回jQuery而不回显?

I am using the ajax() function to send a string to a PHP file.

In case of a success, the PHP file echoes "the validation of the string was successful". But I would also like to send the actual string back to jQuery and put it in a div.

How can send back that string, without having to echo it along with the confirmation message?

Or should I put both messages into a string, send it to jQuery and have jQuery split that string? What's the best way?

  • 写回答

2条回答 默认 最新

  • dongtang1944 2012-04-11 01:03
    关注

    Putting what others have said into code, you might set up your PHP script to return a JSON-encoded object like so:

    $result = array(
      'message' => 'the validation of the string was successful',
      'original' => 'string sent by client'
    );
    
    echo json_encode($result);
    

    On the client side, you can then access each piece from your callback function like so:

    function (result) { 
      alert(result.message);
      // do something with result.original
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?