在javascript中访问php变量,反之亦然

时间:2013-10-17 15:57:12

标签: javascript php

假设我有php页面index.php

<?php
$x=$_POST[sent]  // I get some value in #x
?>
<html>
<head> 
<script>
function getVar()
{
var x= <How to fetch $x from php code>
alert(x);

}
</script>
</head>
<body> 
<button type="submit"> click </buttton>
</body>

</html>

如何在getVar(_)函数中获取$ x? 或者另一种情况是JS中有一些变量然后将它变成PHP代码?

有可能吗?

实际上我有index.php并通过ajax请求加载新页面url.php。我想向url.php发送一些变量,并希望在其中的JS代码中访问它们。

这是我的ajax请求:

 var id=mydata.id; // Guess I am getting some data here
 $.ajax({
                         url:'url.php'
                         ,async:     true
                         ,cache:     false
                         ,dataType:  'html'
                         ,success:   function(data){
                           $('body').html(data);
                           FB.XFBML.parse();
                         }

我想将ajax请求中的id发送到url.php。我将把它作为$x获取,并希望在JS代码中使用它。

4 个答案:

答案 0 :(得分:3)

php to javascript可以使用

完成
<?php $x = "iam a php variable"; ?>
<script>
        function getVar()
        {
        var x= "<?= $x; ?>";
        alert(x);    //alerts **iam a php variable**(value of $x from php)
        }
</script>

反之亦然可以通过 Ajax完成。这是一个简单的例子

<script>
function showUser()
{
var str= "iam a javascript variable";
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    alert(xmlhttp.responseText); //alerts response from php
    }
  }
xmlhttp.open("GET","new.php?q="+str,true); //you can send request through post or get methods 
xmlhttp.send();
}
</script>

在这里,我通过使用GET的ajax在php中获取一个javascript变量。 在new.php中,我只是打印出通过GET收到的请求

<?php 
    print $_GET["q"];    //prints **iam a javascript variable**(value of str from   javascript) 
?>

答案 1 :(得分:1)

PHP和Javascript之间的通信是客户端 - 服务器通信的典型示例。在这种情况下,PHP是您的服务器应用程序,Javascript是您的客户端应用程序。为此通信建立了许多协议,但您应该查看AJAXRESTful服务。

为了理解发生了什么,我建议您从PHP程序员的角度阅读HTTP and REST上的本教程。在设计客户端 - 服务器应用程序时,PHP将通过应用业务逻辑和与数据库通信来“生成”所有内容,并且Javascript会在某些事件(点击,显示表等)上向PHP询问此内容。 PHP而不是输出HTML将输出XML或JSON,因此您可以在Javascript中解析和显示该数据。

答案 2 :(得分:0)

如果您只是想将$x变量的值传递给JavaScript,那么可以按如下方式完成:

function getVar()
{
    var x= "<?php echo htmlspecialchars($x); ?>";
    alert(x);    
}

这是一个有效的例子:

<?php
    $x = $_POST['sent']  // I get some value in #x
?>
<html>

<head> 
    <script>
    function getVar()
    {
        var x= "<?php echo htmlspecialchars($x); ?>";
        alert(x);
    }
    </script>
</head>

<body> 
    <button type="submit" onclick="getVar()"> click </buttton>
</body>

</html>

如果脚本正确收到$x变量,那么当您点击按钮时它将是alert d。

答案 3 :(得分:0)

双向转移:

function getVar($var)
{
var xmlhttp;

    xmlhttp=new XMLHttpRequest();

    xmlhttp.onreadystatechange=function()
      {

      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        $phpVar = xmlhttp.responseText;
            return $phpVar;
        }
      }

    xmlhttp.open("POST","/yourPage.php",true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("var=" + $var);


}

然后在你的page.php上:

<?
if (isset($_POST["var"])) {

  die($_POST["var"]);
  }
?>
相关问题