$ .post()从php接收变量

时间:2013-08-01 14:01:08

标签: php jquery

我从javascript 2值发送到php。

function sendValue(str,str2){
        $.post("/phpfolder/updaterate.php",{ sendValue: str, sendValue2 : str2 },
            function(data){
            $('#display').html(data.returnValue);
            }, "json");
        }

我的php文件执行....

我想发回变量$ x

<?php
...    
echo json_encode($x);
?>

我和哪里缺少什么? 我搜索了一些例子,但没有...

2 个答案:

答案 0 :(得分:1)

json_encode可以将数组作为参数。

您想要显示data.returnValue。所以构造一个这样的数组:

...
echo json_encode( array('returnValue' => $x) );
exit()

答案 1 :(得分:1)

尝试测试这些东西

function sendValue(str,str2){
        $.post("/phpfolder/updaterate.php",{ 'sendValue': str, 'sendValue2' : str2 },//add '  to the name of the variables
            function(data){
            alert('inside the function');//test if is getting inside the function
            $('#display').html(data.returnValue);
            }, "json");
        }

在php中你必须返回一个数组。

<?php
$x['returnValue'] = 'whatever';//The key of the array has to be the name used in the function(data)
echo json_encode($x);
?>