简体   繁体   English

当我尝试在 PHP 中运行此 JS 代码时,为什么没有任何反应?

[英]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.

相关问题 为什么当我点击 onclick 按钮时什么也没有发生? - Why is nothing happening when I click on onclick button? 为什么当我将一些东西推入 JSON 数组时什么也没有发生? - Why is nothing happening when I push something into a JSON array? JS代码应该将数据保存到本地存储; 但什么都没发生 - JS code supposed to save data into local storage; but nothing's happening 当我尝试在“node.js”中运行这行代码时,我收到以下错误 - When I try to run this line of code in “node.js”, I am getting the following error 为什么我在安全帽中运行 js 脚本后什么也没有发生? - Why does nothing happen after I run js scripts in hardhat? 为什么当我尝试将数据推入其中时,我的 state 篮子一直是空的? [反应 JS] - why my state basket stay empty when i try to push data inside ? [REACT JS] 为什么当我尝试在圆圈内填充圆圈时我的代码卡住了? - Why my code get stuck when I try to fill circle inside the circle? 节点。 当我按运行时什么也没有发生(discord.js) - node . when i press run nothing happens (discord.js) 如何在PHP中运行JS? - How do I run JS inside PHP? JS:为什么当我单击按钮时什么都没发生? - JS: Why when I click the button nothing happens?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM