简体   繁体   English

将值从PHP传递到JavaScript

[英]Passing values from PHP into JavaScript

I am trying to open a new window, with its content coming from PHP code. 我正在尝试打开一个新窗口,其内容来自PHP代码。 I need to have the value $MsgWindow passed to the: myWindow.document.write(something); 我需要将值$MsgWindow传递给: myWindow.document.write(something); statement in the JavaScript. JavaScript中的语句。 So far I've been unsuccessful in doing it. 到目前为止,我一直没有成功。 Any suggestions? 有什么建议么?

<!DOCTYPE html>
<html>
<?php>
     $info = array($_GET["airport"],
                   $_GET["state"],
                   $_GET["distance"],
                   $_GET["price"]);
    $fin=array();
    foreach ($info as $value) {
        $value = substr($value, 2);
        $value=substr($value, 0, -2);
        array_push($fin,$value);
    }
    $airport = $fin['0'];
    $state   = $fin['1'];
    $distance= $fin['2'];
    $price   = $fin['3'];
    $MsgWindow="Your estimate to ".$airport."  ".$state. " is ".$price;
    echo $MsgWindow;
    echo "<br>";
?>
<p>Click the button to write some text in the new window and the source (parent) window.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
   var myWindow = window.open("", "myWindow", "width=200, height=100");
    myWindow.document.write(something);
    myWindow.opener.document.write("<p>This is the source window!</p>");
}
</script>
</html>

You could do the following. 您可以执行以下操作。 The variable is proccessed by php, and it appears as valid javascript code: 该变量由php处理,并显示为有效的javascript代码:

<script type="text/javascript">
   var php_var = "<?php echo $MsgWindow; ?>";
</script>

I would like to note that if $MsgWindow has quotes, it will break the script. 我想指出的是,如果$MsgWindow带有引号,它将破坏脚本。 You will have to use addshlashes to get around that. 您将必须使用addshlashes来解决该问题。

Because you are building the javascript code as part of the execution of the PHP script, you can simply echo out the PHP variable where you want to use it in the javscript fragment. 因为要在执行JavaScript脚本的过程中构建javascript代码,所以可以简单地在要在javscript片段中使用PHP变量的地方回显PHP变量。

Like this : 像这样 :

<!DOCTYPE html>
<html>
<?php
     $info = array($_GET["airport"],
                   $_GET["state"],
                   $_GET["distance"],
                   $_GET["price"]);
    $fin=array();
    foreach ($info as $value) {
        $value = substr($value, 2);
        $value=substr($value, 0, -2);
        array_push($fin,$value);
    }
    $airport = $fin['0'];
    $state   = $fin['1'];
    $distance= $fin['2'];
    $price   = $fin['3'];
    $MsgWindow="Your estimate to $airport $state is $price";
    echo $MsgWindow;
    echo "<br>";
?>
<p>Click the button to write some text in the new window and the source (parent) window.</p>

<button onclick="myFunction()">Try it</button>

<script type="text/javascript">
function myFunction()
{
   var myWindow = window.open("", "myWindow", "width=200, height=100");

       // Change here
       myWindow.document.write("<?php echo $MsgWindow; ?>");
       myWindow.opener.document.write("<p>This is the source window!</p>");
}
</script>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM