【发布时间】:2021-01-14 03:57:29
【问题描述】:
我正在尝试将 $php_variable 传递给 JavaScript 内容在一个 php 文件中的 echo 标记中。这是因为我希望通过使用window.location 将查询值从old_page_url 附加到new_page_url。
例如,
http://example/?data=1234 到 http://example/something-else/1234
我的代码sn-p:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + $php_variable";
echo "</script>";
我试过了:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + json_decode($php_variable)";
echo "</script>";
和
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + <?php echo $php_variable; ?>";
echo "</script>";
但这两个不起作用。有什么想法吗?
更新:最终代码片段
<?php
$php_variable = "testing";
$toURL = "http://example.com/something-else";
echo "<script>";
echo "window.location = '$toURL/$php_variable'";
echo "</script>";
【问题讨论】:
标签: javascript php query-string key-value