【问题标题】:$.post() receive variable back from php$.post() 从 php 接收变量
【发布时间】:2013-08-01 14:01:08
【问题描述】:

我从 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);
?>

我在哪里以及缺少什么? 我搜索了示例,但没有...

【问题讨论】:

标签: php jquery


【解决方案1】:

json_encode 可以将数组作为参数。

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

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

【讨论】:

  • 那么你知道$x 是什么吗? -1
  • 从上面OP的评论来看,它是一个字符串。
  • thks,我很困惑 function(data){ $('#display').html(data.returnValue);我现在明白了
  • 好的。我建议您阅读如何使用 $.ajax(和其他 jQuery Ajax 函数)、如何构造和解析 JSON 对象以及如何使用 firebug/webkit 控制台来调试 ajax/http 请求。
【解决方案2】:

尝试测试这些东西

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);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2012-08-02
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多