如何将PHP变量传递回调用它的Javascript页面

如何将PHP变量传递回调用它的Javascript页面,php,javascript,variables,Php,Javascript,Variables,目前,我有一个Javascript文件(在客户机上运行),我从中调用一个PHP文件(在服务器上)。PHP文件完成后,我想将三个变量传递回客户端上运行的javascript。我知道了如何让javascript等待php文件完成执行,所以这不是问题所在。问题是将变量传递回javascript文件。这能做到吗?我看到的所有示例都有某种混合javascript/php文件。我希望找到一些方法来传递php变量,就像jquery.ajax一样。我不知道这是否可以做到,因为我对Javascript和PHP太陌

目前,我有一个Javascript文件(在客户机上运行),我从中调用一个PHP文件(在服务器上)。PHP文件完成后,我想将三个变量传递回客户端上运行的javascript。我知道了如何让javascript等待php文件完成执行,所以这不是问题所在。问题是将变量传递回javascript文件。这能做到吗?我看到的所有示例都有某种混合javascript/php文件。我希望找到一些方法来传递php变量,就像jquery.ajax一样。我不知道这是否可以做到,因为我对Javascript和PHP太陌生了。 谢谢

javascript:

<html>
    <head>
        <title>Login Page</title>
    </head>
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script language="Javascript">
        function calculations(callback)
        {
            var x = <?php echo $username?>;
            alert(x);
            var password = window.prompt("Please Type Your Password");
        }//ends the calculations function
        function getUsername()
        {
            var username = window.prompt('Please Type Your Username');
            var temp = document.getElementById('temp');
            temp.innerHTML = username;
            jQuery.ajax(
                {
                    type: "POST",
                    url:"GetInfo.php",
                    data: "username="+username,
                    success: function(msg)
                            {callback.call();}

                });//ends the jQuery send


        }//ends the GetUsername function
    </script>
    <body onLoad=getUsername()>
        <div id="temp">This will show text</div>

    <body>

</html>

登录页面
函数计算(回调)
{
var x=;
警报(x);
var password=window.prompt(“请键入密码”);
}//结束计算功能
函数getUsername()
{
var username=window.prompt('请键入您的用户名');
var temp=document.getElementById('temp');
temp.innerHTML=用户名;
jQuery.ajax(
{
类型:“POST”,
url:“GetInfo.php”,
数据:“用户名=”+用户名,
成功:功能(msg)
{callback.call();}
});//结束jQuery发送
}//结束GetUsername函数
这将显示文本
php:


要将数据传递回调用脚本的Javascript,只需如下所示:

$value = 'foobar';
echo $value;
对于多个值,可以将其作为JSOn对象传回

$name = 'foobar';
$text = 'helloworld';
$value = 5;

//add to associative array

$result['name'] = $name;
$result['text'] = $text;
$result['value'] = $value;

// encode as json and echo
echo json_encode($result);
在Javascript端,您的回调函数将接收以下内容:

function callback(data){
    var result = JSON.parse(data);
    //access the members
    console.log(result.name);
    console.log(result.text);
    console.log(result.value);
}

看起来像是复制品。。。我还想补充一点,您应该检查JS数组和JSON,可能是通过PHP的
JSON\u encode
方法。@Edwin我很确定您提供的链接与希望将javascript变量发送到PHP页面的人正好相反。海报应该使用ajax。不过,我肯定会研究你说的其他内容。我尝试了你的答案,但有一个错误告诉我在语句
result=JSON.parse(data)中遇到了意外字符
在上面的注释中,我指的是
var result=JSON.parse(data)Sry我现在知道了。我在放
JSON.parse(数据)在错误的函数中。不经意间,
回调
函数是jquery函数,而不是之前的函数。请注意,在Internet Explorer中,JSON对象是从IE 8开始提供的。如果您喜欢IE7,可以使用jQuery的jQuery.parseJSON作为替代。
function callback(data){
    var result = JSON.parse(data);
    //access the members
    console.log(result.name);
    console.log(result.text);
    console.log(result.value);
}