I am trying to pass $php_variable to JavaScript content in echo tag in one php file. It is because I hope to append the query value from old_page_url to new_page_url by using window.location
.
For example,
http://example/?data=1234
to http://example/something-else/1234
My code snippet:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + $php_variable";
echo "</script>";
I have tried:
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + json_decode($php_variable)";
echo "</script>";
and
<?php
$php_variable = "testing";
echo "<script>";
echo "var toURL = 'http://example.com/something-else/'";
echo "window.location = toURL + <?php echo $php_variable; ?>";
echo "</script>";
But these two don't work. Any idea?
Update: Final Code Snippet
<?php
$php_variable = "testing";
$toURL = "http://example.com/something-else";
echo "<script>";
echo "window.location = '$toURL/$php_variable'";
echo "</script>";
question from:
https://stackoverflow.com/questions/65713228/pass-php-variable-within-echo-script-content 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…