我的文件中有以下代码test.php。该代码主要有一个在 PHP 中定义的变量,我想将它发送到 JavaScript 函数以便它可以 POST。这是代码:
<!DOCTYPE html>
<html>
<head>
<title>Payment Receipt</title>
</head>
<body>
<?php
...
if($row) {
$myValue = 'Hi there!'; //the PHP variable (currently has a sample value, but will be string only)
//JS starts
echo <<<JS001
<script type="text/javascript">
var msg = {$myValue}; //trying to set it to a JS var, so I could send it to below function.
var ThunkableWebviewerExtension = {
postMessage: function (message) {
if (window.ReactNativeWebView) {
window.ReactNativeWebView.postMessage(message);
} else {
window.parent.postMessage(message, '*');
}
}
};
ThunkableWebviewerExtension.postMessage(msg);
console.log(msg);
alert('Message Sent');
</script>
JS001;
} else {
echo 'Incorrect Value';
}
?>
</body>
</html>
但是当我运行这段代码时,我在控制台上收到了这个错误:Uncaught SyntaxError: Unexpected identifier。如果我只想向JS代码发送一个简单的字符串值怎么办?目前有什么问题吗?
任何帮助表示赞赏!谢谢!
杨__羊羊
千万里不及你
幕布斯6054654
长风秋雁
相关分类