簡體   English   中英

如何將普通變量的值從 PHP 發送到 JavaScript?

[英]How to send a normal variable's value from PHP to JavaScript?

我的test.php文件中有以下代碼段。 該代碼主要有一個在 PHP 中定義的變量,我想將它發送到一個 JavaScript 函數,以便它可以 POST 它。 這是代碼:

<!DOCTYPE html>
<html lang="en">
<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 代碼發送一個簡單的字符串值,我該怎么辦? 目前有什么問題?

任何幫助表示贊賞! 謝謝!

你可以做:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Payment Receipt</title>
</head>

<body>

<?php
...
if($row) {
    $myValue = 'Hi there!';
?>
<script>
var msg = "<?php echo $myValue; ?>"; //trying to set it to a JS var, so I could send it to below
//Your remaining js script here ...

</script>
<?php } else { 
//your else condition
}
?>
</body>
</html>

嘗試這個

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Payment Receipt</title>
</head>

<body>

<?php
...
if($row) {
    $myValue = 'Hi there!'
?>
<script>
var msg = "<?php $myValue; ?>";

</script>
<?php } else { 
  echo 'Incorrect Value';
}
?>
</body>
</html>

簡化你的代碼(調試時你應該總是這樣做!)你有這個:

$myValue = 'Hi there!';
echo <<<JS001
<script type="text/javascript">
var msg = {$myValue};
</script>
JS001;

如果您查看返回到瀏覽器的 HTML,您會看到它如下所示:

<script type="text/javascript">
var msg = Hi there!;
</script>

瀏覽器不知道“你好!” 應該是一個字符串,所以它嘗試將它作為代碼執行。

你想要的輸出是這樣的:

<script type="text/javascript">
var msg = 'Hi there!';
</script>

所以我們需要將這些引號添加到 PHP 中:

$myValue = 'Hi there!';
echo <<<JS001
<script type="text/javascript">
var msg = '{$myValue}';
</script>
JS001;

作為更通用的解決方案,您可以濫用 JSON 字符串是有效的 JS 值這一事實,並在 PHP 中使用json_encode

$myValue = 'Hi there!';
$myValueJson = json_encode($myValue);
echo <<<JS001
<script type="text/javascript">
var msg = {$myValueJson};
</script>
JS001;

這在這種情況下沒有區別,但對於傳遞其他類型的值非常有用 - 數組、 null

為了更好的代碼管理,您可能應該將 HTML、PHP 和 JS 代碼分開放在不同的文件中。

想象一下這樣的事情:

控制器.php


$displayName="David";

include 'vue.php';

Vue.php


<html>
...
<body>

<div id="php-data" data-displayName="<?php echo $displayName ?>"></div>

</body>
</html>

腳本.js

<script>
var msg = document.getElementById('php-data').dataset.displayName // "David";

</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM