将值从PHP传递到JavaScript
我试图打开一个新窗口,其内容来自PHP代码。我需要将值将值从PHP传递到JavaScript,javascript,php,Javascript,Php,我试图打开一个新窗口,其内容来自PHP代码。我需要将值$MsgWindow传递给: myWindow.document.write(某物)语句。 到目前为止,我还没有成功。 有什么建议吗 <!DOCTYPE html> <html> <?php> $info = array($_GET["airport"], $_GET["state"], $_GET["distance"]
$MsgWindow
传递给:
myWindow.document.write(某物)语句。
到目前为止,我还没有成功。
有什么建议吗
<!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>
单击按钮在新窗口和源(父)窗口中写入一些文本
试试看
函数myFunction()
{
var myWindow=window.open(“,”myWindow“,”宽度=200,高度=100”);
myWindow.document.write(某物);
myWindow.opener.document.write(“这是源窗口!”;
}
您可以执行以下操作。该变量由php处理,并显示为有效的javascript代码:
<script type="text/javascript">
var php_var = "<?php echo $MsgWindow; ?>";
</script>
var php_var=”“;
我想指出的是,如果$MsgWindow
有引号,它将破坏脚本。您必须使用addshlashes来解决这个问题。因为您正在构建javascript代码,作为PHP脚本执行的一部分,您只需在javscript片段中要使用的地方回显PHP变量即可
像这样:
<!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>
单击按钮在新窗口和源(父)窗口中写入一些文本
试试看
函数myFunction()
{
var myWindow=window.open(“,”myWindow“,”宽度=200,高度=100”);
//在这里换车
myWindow.document.write(“”);
myWindow.opener.document.write(“这是源窗口!”;
}
myWindow.document.write(“”)
UhmmyWindow.document.write(“”)代码>?此这是本案例的正确答案。另一种方法是将文本添加到路径URL中,并使用JS操纵该URL字符串以获得所需内容。var myText_var=$MsgWindow;函数myFunction(){var myWindow=window.open(“,”myWindow“,”width=200,height=100“);myWindow.document.write(myText\u var);myWindow.opener.document.write(“这是源窗口!”)}