Pass php variable to javascript

Asked
Active3 hr before
Viewed126 times

4 Answers

variablejavascript
90%

Echo the data into the page somewhere, and use JavaScript to get the information from the DOM,,Use AJAX to get the data you need from the server

Example_snippet/controller/utility/_variable.js/ /* Do some operation here, lik. . .
/* Do some operation here, like talk to the database, the file-session
 * The world beyond, limbo, the city of shimmers, and Canada.
 *
 * AJAX generally uses strings, but you can output JSON, HTML and XML as well.
 * It all depends on the Content-type header that you send with your AJAX
 * request. */

echo json_encode(42); // In the end, you need to echo the result.
// All data should be json_encode()d.

// You can json_encode() any value in PHP, arrays, strings,
//even objects.
Step 2 continued with <!-- snip --> <script> fun. . .
<!-- snip -->
<script>
    function reqListener () {
      console.log(this.responseText);
    }

    var oReq = new XMLHttpRequest(); // New request object
    oReq.onload = function() {
        // This is where you handle what to do with the response.
        // The actual data is found on this.responseText
        alert(this.responseText); // Will alert: 42
    };
    oReq.open("get", "get-data.php", true);
    //                               ^ Don't block the rest of the execution.
    //                                 Don't wait until the request finishes to
    //                                 continue.
    oReq.send();
</script>
<!-- snip -->
Step 3 continued with <!-- snip --> <div id="dom-tar. . .
<!-- snip -->
<div id="dom-target" style="display: none;">
   <?php
        $output = "42"; // Again, do some operation, get the output.
        echo htmlspecialchars($output); /* You have to escape because the result
                                           will not be valid HTML otherwise. */
    ?>
</div>
<script>
   var div = document.getElementById("dom-target");
   var myData = div.textContent;
</script>
<!-- snip -->
Step 4 continued with <!-- snip --> <script> var. . .
<!-- snip -->
<script>
   var data = < ? php echo json_encode("42", JSON_HEX_TAG); ? > ; // Don't forget the extra semicolon!
</script>
<!-- snip -->
load more v
88%

Search Answer Titles

Example_snippet/controller/utility/_variable.js/ <script> 'var name = <?php ech. . .
<script>
   'var name = <?php echo json_encode($name); ?>;
</script>'
load more v
72%

PHP is server side language which retrieves data from database and show in client side, Javascript is client side language which manipulate HTML and data receives from server

Example_snippet/controller/utility/_javascript.js/ <?php     $message = "Hello Wo. . .
<?php
    $message = "Hello World!"; // here you get data from database
?>
<!DOCTYPE html>
<html>

<head>
       <title>PHP variable to JavaScript</title>
</head>

<body>
       <h1>PHP variable to JavaScript</h1>    
       <script type="text/javascript">
      var message = "<?php echo"
      $message "?>";
      alert(message);
   </script>
</body>

</html>
load more v
65%

For something seemingly so straight-forward, you would think that the information out there would be a bit better on this subject, Passing a variable from PHP to JavaScript is actually very easy! There are just a couple things you need to keep in mind and some ways to do it to ensure it actually works

Example_snippet/controller/utility/_javascript.js/ <head>. . .
<head>