-1

So I have an index.html with the following code below. I want to get the variable "variableFromMyPHPscript" into my index.html from my GetVariable.php file.

    <!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.

    <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? Thank you for any help!

Cam G
  • 107
  • 1
  • 9
  • 1
    use ajax request to get php variable in jquery/javascript – Pardeep Poria May 13 '16 at 16:36
  • Php script can't be placed between script tags like you did. Have a look at this : http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Louys Patrice Bessette May 13 '16 at 16:42
  • Possible duplicate of [How to access PHP variables in JavaScript or jQuery rather than ](http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php-echo-vari) – Ani Menon May 13 '16 at 16:43

2 Answers2

0

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. 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.

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. 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:

Ajax tutorial for post and get

Community
  • 1
  • 1
Geoherna
  • 3,249
  • 1
  • 24
  • 38
0

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

<?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>    
obsergiu
  • 340
  • 3
  • 12