无法获取从 PHP 到 Javascript 的值

Cannot get value from PHP to Javascript

因此,在我的 PHP 文件中,我创建了一个 JSON 对象,我需要将其转到我的 Javascript 文件进行处理。但是,我似乎无法传输任何内容,即使使用这样的简单字符串也无法传输:-

index.php

<?php
  $cookie= "blueberry cake";
?>;

script.js

   var details = "<?php echo $cookie; ?>";
   function myFunction() { 
        document.getElementById("demo").innerHTML = details;
   }

index.html

<html>

<head>
    <script src="script.js"></script>
    <title>antgus</title>
</head>
<body>

    <h1>Welcome to my page! </h1>

    <button type="button" onclick="myFunction()">Try it</button>

    <p id="demo">A Paragraph.</p>


</body>

我的 HTML 中有一个按钮,所以当我按下它时,调用函数 myFunction。它应该将 ID 为 "demo" 的 p 元素更改为字符串 "blueberry cake"

知道我做错了什么吗?

编辑

我现在可以正常工作了,除了一件事,我无法将 JSON 对象传递给函数 i Javascript。控制台抛出一个错误,指出参数错误,它认为我正在传递多个字符串。 index.php

    <?php
    $test = "blue";
    $logLines = file('../../../home/shares/flower_hum/humid.log');
    $entries = array_map("clean",$logLines);
    $finalOutput = [
        'humid'   => $entries
    ];
    function clean($string){
        return json_decode(rtrim(trim($string),','),true);
    }

    $json = json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
    echo $json; //displays valid JSON, I have checked it.
?>

<html>
    <head>
        <script src="script.js"></script>
        <title>antgus</title>
    </head>
    <body>
        <p>Welcome to my page! Down below you can see the data:</p>
        <button type="button" onclick='myFunction("<?php echo $json ?>")'>Try it</button>
        <p id="demo">A Paragraph.</p>
    </body>
</html>

script.js

function myFunction(jsonObject) {
    document.getElementById("demo").innerHTML = "INCOMING DATA:";
    alert(jsonObject);
}
    enter code here

外部 js 文件不会被解析为 php,这意味着您无法访问该变量。您可以将您的 js 保存为 php(不推荐)。或者 a)

在 php 页面底部包含该功能:

<?php
  $cookie= "blueberry cake";
?>;
    //js further down the same page
  <script>
    var details = "<?php echo $cookie; ?>";
   function myFunction() { 
        document.getElementById("demo").innerHTML = details;
    }
 </script>

或者b)在php页面声明一个全局变量,以便外部js可以访问:

//php page
    <?php
      $cookie= "blueberry cake";
    ?>;
<script> var details = "<?php echo $cookie; ?>";</script>


//js file
       function myFunction() { 
          //details will be accessible since it has been declared in the global space of the index.php page
            document.getElementById("demo").innerHTML = details;
       }

这应该可以完美运行:

With In-Document JS:

<?php
     $cookie= "blueberry cake";
?>;

<html>

    <head>
        <script type="text/javascript">
            var details = "<?php echo $cookie; ?>";
            function myFunction() {
                document.getElementById("demo").innerHTML = details;
            }
        </script>
        <title>antgus</title>
    </head>
    <body>

        <h1>Welcome to my page!</h1>
        <button type="button" onclick="myFunction()">Try it</button>
        <p id="demo">A Paragraph.</p>
    </body>

With External JS:

<?php
     $cookie= "blueberry cake";
?>;

<html>

    <head>
        <script type="text/javascript">
            var details = "<?php echo $cookie; ?>";
        </script>
        <script type="text/javascript" src="script.js"> </script>
        <title>antgus</title>
    </head>
    <body>

        <h1>Welcome to my page!</h1>
        <button type="button" onclick="myFunction()">Try it</button>
        <p id="demo">A Paragraph.</p>
    </body>

script.js File:

    function myFunction() {
         // HAS ACCESS TO details WHICH IS ON THE GLOBAL SCOPE
         document.getElementById("demo").innerHTML = details;
    }

使用json_encode对json对象进行编码,即

<script> var details = "<?php echo json_encode($cookie); ?>";</script>

需要考虑的事项:-

1.External js 将无法像您尝试的那样工作。

2.When 您直接通过 onclick 代码调用 javascript 函数,然后您还必须传递数据才能进一步工作。

工作代码示例:-

index.php:-

<?php
  $cookie= "blueberry cake";
?>
<html>

<head>
    <script src="script.js"></script>
    <title>antgus</title>
</head>
<body>
<h1>Welcome to my page! </h1>

<button type="button" onclick='myFunction("<?php echo $cookie; ?>")'>Try it</button>

<p id="demo">A Paragraph.</p>

script.js:-

function myFunction(details) { 
    document.getElementById("demo").innerHTML = details;
}

本地端输出:-

点击前:- http://prntscr.com/ci94s9

点击后:-http://prntscr.com/ci94w9

删除 php 周围的引号,伙计。您分配的是字符串而不是 php 变量值。

var details = "'" + <?php echo $cookie; ?> + "'";

这将分配实际的 $cookie 值而不是字符串 '<? php echo $cookie; ?>'

php-脚本是运行服务器端,javascript是运行客户端,两者不共享某种范围.

换句话说,$cookie 变量在 php 中赋值,但未使用。然后你在 javascript 中使用一个同名变量(但与 php 变量完全无关),但由于它从未被分配任何东西,所以它是 undefined.

gavgrif 提供了一些解决方案来解决这个问题。