[英]Why is nothing happening when I try to run this JS code inside PHP?
I have a file named test.php
which has this whole code:我有一个名为
test.php
的文件,其中包含完整的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Payment Receipt</title>
</head>
<body>
<?php
...
$result = $stmt->get_result();
$row = $result->fetch_assoc();
if($row) {
$obj->txndate = $row['date'];
$obj->txnid = $row['txnid'];
$obj->atomid = $row['atomid'];
$obj->amount = $row['amount'];
$myJSON = json_encode($obj);
echo $myJSON;
//javascript starts inside PHP
echo '<script>', PHP_EOL;
echo 'var msg = <?php echo json_encode($myJSON); ?>;', PHP_EOL;
echo 'var ThunkableWebviewerExtension = {', PHP_EOL;
echo 'postMessage: function (message) {', PHP_EOL;
echo 'if (window.ReactNativeWebView) {', PHP_EOL;
echo 'window.ReactNativeWebView.postMessage(message);', PHP_EOL;
echo '} else {', PHP_EOL;
echo 'window.parent.postMessage(message, '*');', PHP_EOL;
echo '}', PHP_EOL;
echo '}', PHP_EOL;
echo '}', PHP_EOL;
echo 'ThunkableWebviewerExtension.postMessage(msg);', PHP_EOL;
echo '</script>', PHP_EOL;
} else {
echo 'Incorrect ID';
}
$conn->close();
?>
</body>
</html>
When I run this code, nothing happens in the JavaScript side - everything works well until the JavaScript starts.当我运行此代码时,JavaScript 端没有任何反应 - 在 JavaScript 启动之前一切正常。 This is the JavaScript code I tried to embed in the PHP code:
这是我尝试嵌入到 PHP 代码中的 JavaScript 代码:
<!DOCTYPE html>
<html>
<body>
<?php
$myJSON = 'Hi there!';
?>
<script>
var msg = <?php echo json_encode($myJSON); ?>;
var ThunkableWebviewerExtension = {
postMessage: function (message) {
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(message);
} else {
window.parent.postMessage(message, '*');
}
}
}
ThunkableWebviewerExtension.postMessage(msg);
</script>
</body>
</html>
The above code works well as expected when I run it separately in a different file, which too has the PHP variable passed in the var msg
command.当我在不同的文件中单独运行上面的代码时,上面的代码按预期运行,该文件也具有在
var msg
命令中传递的 PHP 变量。
I wonder what I did wrong trying to write JavaScript code inside PHP - I saw somewhere that by echoing the statement, then adding PHP_EOL
at the end we can write JS/HTML in-between PHP.我想知道我在尝试在 PHP 中编写 JavaScript 代码时做错了什么 - 我在某处看到通过回显该语句,然后在末尾添加
PHP_EOL
,我们可以在 Z2FEC392304A5C23AC138DA228 之间编写 JS/HTML
What's wrong here?这里有什么问题? Any help would be appreciated!
任何帮助,将不胜感激!
This does not work:这不起作用:
echo '<script>', PHP_EOL;
echo 'var msg = <?php echo json_encode($myJSON); ?>;', PHP_EOL;
because it sends PHP code to the client , which will not execute it since it has no PHP parser.因为它将 PHP 代码发送到客户端,客户端不会执行它,因为它没有 PHP 解析器。
Write it like this:像这样写:
$jsonEncoded = json_encode($myJSON);
echo <<<JS001
<script>
var msg = {$jsonEncoded};
var ThunkableWebviewerExtension = {
postMessage: function (message) {
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(message);
} else {
window.parent.postMessage(message, '*');'
}
}
};
ThunkableWebviewerExtension.postMessage(msg);
</script>
JS001;
// The line above must end in ";" (not even spaces after) and the J must start
// on the very first columns.
Other sections in the same file would be called JS002, and so on.同一文件中的其他部分将称为 JS002,依此类推。 Inside each "here-document" section, if you want to embed a php variable write it like
{$variable}
, and if you have a $
sign otherwise, escape it with a backslash, \\$
.在每个“此处文档”部分中,如果您想嵌入一个 php 变量,请将其写为
{$variable}
,否则,如果您有$
符号,请使用反斜杠\\$
其转义。
echo '<script>
alert("hhh");
</script>';
? >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.