【问题标题】:Using variable on php to create another variable in JS在 php 上使用变量在 JS 中创建另一个变量
【发布时间】:2018-11-23 05:29:46
【问题描述】:

我很难在 php 中获取特定变量的值以在 js 中使用。这是我在 php 中的代码:

<?php
require("connection.php");

$sql_cmd = "SELECT * FROM tbstatus";

$stmt = $con->prepare($sql_cmd);
$stmt->execute();

echo "<h2>STATUS OF THE BABY</h2>";

while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo "<h4>" . $result['status'] . "</h4>";
}
?>

我想获取 this ($result['status']) 的值并将其传递给 js 中的变量 pos。这是我的js代码:

setInterval(function() {
    $("#position").load('refresh.php');
    notif();

}, 1000);

function notif() {
    var pos = $('PHP VARIABLE HERE').val();
    alert(pos);
}

感谢您的帮助。

【问题讨论】:

标签: javascript php jquery database


【解决方案1】:

最简单的方法是直接输出到javascript:

?>
<script type="text/javascript">
window.MY_PHP_VAR = <?php echo json_encode($myPhpVar); ?>;
</script>
...

window.MY_PHP_VAR now contains your php variable

【讨论】:

    【解决方案2】:

    如果您的 javascript 代码在结果出现的同一页面上,那么您可以使用它

    var pos =  `<?php echo $result['status'] ?>`;
    
    var pos =  `<?= $result['status'] ?>`;
    

    【讨论】:

      【解决方案3】:
      ===============
      // refresh.php
      ===============
      
      <?php  
      require("connection.php");
      $sql_cmd = "SELECT * FROM tbstatus";
      $stmt = $con->prepare($sql_cmd);
      $stmt->execute();
      
      echo "<h2>STATUS OF THE BABY</h2>";
      
      while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<h4>" . $result['status'] . "</h4>";
            echo "<script> alert('". $result['status'] ."'); </script>";
            /* If the $result['status'] is 'success' the above line will be converted to:
             echo "<script> alert('success'); </script>";
            */
      }
      
      ?>
      

      所以,每次 refresh.php 加载时,脚本都会被执行。

      但是,我建议您为您的 h4 分配一个 id 或 class 属性,您可以在其中回显您的状态并使用 javascript 中的选择器访问该值。

      【讨论】:

        【解决方案4】:
        1. 您可以尝试为 status 提供 id 或 class。
        2. 然后在 JavaScript 中,您可以获取 id 或类的值。

        PHP

        <?php
        require("connection.php");
        
        $sql_cmd = "SELECT * FROM tbstatus";
        
        $stmt = $con->prepare($sql_cmd);
        $stmt->execute();
        
        echo "<h2>STATUS OF THE BABY</h2>";
        while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo '<h4> <span class="status">' . $result['status'] . '</span></h4>';
        }
        
        ?>
        

        JavaScript

        var oldStatus = '';
        
        setInterval(function() {
            $("#position").load('refresh.php');
            notif();
        }, 1000);
        
        function notif() {
            // note that since we used a class, you will get the value of the first element only.
            var pos = $('.status').text(); // use .text() instead of .val()
            if (pos.toLowerCase() == 'out' && pos != oldStatus){
                oldStatus = pos;
                alert(pos);
            }
        }
        

        【讨论】:

        • 它正在工作。谢谢你。但我有点担心。由于我在我的 js 中有 setInterval,它每秒都会提醒我状态的值。我想要的只是提醒我一次,如果状态的值没有改变。是否可以?谢谢
        • setTimeout替换setInterval
        • 如果我使用 setTimeout,它不会自动刷新我的网页。我需要它来从我的数据库中获取最新值
        • 我已经修改了我的答案。看看吧。
        • 我不明白这个。我希望它在状态为“Out”时通知我。
        猜你喜欢
        • 2016-02-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        • 2019-07-31
        • 2012-04-19
        • 2016-02-25
        • 1970-01-01
        相关资源
        最近更新 更多