简体   繁体   English

从 PHP 脚本获取变量到 HTML 脚本?

[英]Get variables from PHP script to HTML script?

So I have an index.html with the following code below.所以我有一个带有以下代码的 index.html 。 I want to get the variable "variableFromMyPHPscript" into my index.html from my GetVariable.php file.我想从我的 GetVariable.php 文件中将变量“variableFromMyPHPscript”放入我的 index.html 中。

    <!doctype html>
<html>

<head>
    <meta charset = "utf-8">  
</head>

<body>
        <p>Random Text....</p>
</body>

</html>

<script>

      console.log(variableFromMyPHPscript);

</script>

And here is my PHP script (GetVariable.php) which uses json to get a value from my database so that it can be stored as a variable.这是我的 PHP 脚本 (GetVariable.php),它使用 json 从我的数据库中获取一个值,以便将其存储为变量。

    <script>

   var variableFromMyPHPscript = <?php

           $link = mysqli_connect('....', '....', '....', '....');
           $query = "select * from thunderDemo";
           $result = mysqli_query($link, $query);

           while($row = mysqli_fetch_array($result))
           {
                   if($row["ThunderOrNot"] == 'Thunder')
                   {
                       echo json_encode("Thunder1");
                   }
           }

                mysqli_close($link); 
           ?>;

</script>

So how would I go about getting the value of "variableFromMyPHPscript" from my PHP file to my HTML file so that I can use it with the JavaScript within the HTML file?那么我如何将“variableFromMyPHPscript”的值从我的 PHP 文件获取到我的 HTML 文件,以便我可以将它与 HTML 文件中的 JavaScript 一起使用? Thank you for any help!感谢您的任何帮助!

So i'm not sure if you're entirely aware of this, but PHP typically either outputs HTML (since PHP files can contain HTML) but this is a sort of older way of doing things.所以我不确定您是否完全意识到这一点,但 PHP 通常要么输出 HTML(因为 PHP 文件可以包含 HTML),但这是一种较旧的做事方式。 A better approach would be using some sort of MVC design pattern where you have designated views which receive their data from a controller, etc.更好的方法是使用某种 MVC 设计模式,您可以指定从控制器等接收数据的视图。

If you are just looking for a quick and dirty fix for getting data from a php script to HTML, the comment above which mentions AJAX would probably be your best bet.如果您只是在寻找将数据从 php 脚本获取到 HTML 的快速而肮脏的解决方案,那么上面提到 AJAX 的评论可能是您最好的选择。 I would use something like JQuery on the frotnend to make an AJAX request to the PHP file and display the data in HTML/ Here is a good link on how to do that:我会在前端使用类似 JQuery 的东西向 PHP 文件发出 AJAX 请求并以 HTML/ 显示数据,这是一个关于如何做到这一点的好链接:

Ajax tutorial for post and get 用于发布和获取的 Ajax 教程

The easiest approach is to have an index.php file in which to encode the result in JSON and echo it to HTML/JS, something like:最简单的方法是拥有一个index.php文件,在其中将结果编码为 JSON 并将其回显到 HTML/JS,例如:

index.php索引.php

<?php
       $link = mysqli_connect('....', '....', '....', '....');
       $query = "select * from thunderDemo";
       $result = mysqli_query($link, $query);
       $content = '';          

       while($row = mysqli_fetch_array($result))
       {
               if($row["ThunderOrNot"] == 'Thunder')
               {
                   $content .= "Thunder1";
               }
       }

            mysqli_close($link); 
?>
<!doctype html>
<html>

<head>
    <meta charset = "utf-8">  
</head>

<body>
        <p>Random Text....</p>
</body>

</html>
<script>
      var variableFromMyPHPscript = "<?php echo json_encode($content); ?>";
      console.log(variableFromMyPHPscript);

</script>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM