Pass php variable to javascript

Active3 hr before
Viewed126 times

4 Answers


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 -->
    function reqListener () {

    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
    };"get", "get-data.php", true);
    //                               ^ Don't block the rest of the execution.
    //                                 Don't wait until the request finishes to
    //                                 continue.
<!-- snip -->
Step 3 continued with <!-- snip --> <div id="dom-tar. . .
<!-- snip -->
<div id="dom-target" style="display: none;">
        $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. */
   var div = document.getElementById("dom-target");
   var myData = div.textContent;
<!-- snip -->
Step 4 continued with <!-- snip --> <script> var. . .
<!-- snip -->
   var data = < ? php echo json_encode("42", JSON_HEX_TAG); ? > ; // Don't forget the extra semicolon!
<!-- snip -->
load more v

Search Answer Titles

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

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. . .
    $message = "Hello World!"; // here you get data from database
<!DOCTYPE html>

       <title>PHP variable to JavaScript</title>

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

load more v

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