Skip to content

Instantly share code, notes, and snippets.

@daveh
Created February 17, 2021 19:28
Show Gist options
  • Save daveh/56fd2fa7801dbfb457f9fe626bfefc48 to your computer and use it in GitHub Desktop.
Save daveh/56fd2fa7801dbfb457f9fe626bfefc48 to your computer and use it in GitHub Desktop.

Revisions

  1. daveh created this gist Feb 17, 2021.
    7 changes: 7 additions & 0 deletions data.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    <?php

    $name = "David \"Dave\" O'Connor";

    header('Content-Type: application/json');

    echo json_encode($name);
    32 changes: 32 additions & 0 deletions output-into-dom.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?php

    $name = "David \"Dave\" O'Connor";

    ?><!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>PHP Data in JavaScript</title>
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    <meta name="name" content="<?= htmlspecialchars($name) ?>">
    </head>
    <body>

    <h1 data-name="<?= htmlspecialchars($name) ?>">Demo</h1>

    <input type="hidden" id="name" value="<?= htmlspecialchars($name) ?>">

    <script>

    //var name = document.querySelector('input[type="hidden"').value;

    //var name = document.querySelector('meta[name="name"]').content;

    var name = document.querySelector('h1').dataset.name;

    alert('Hello ' + name);

    </script>

    </body>
    </html>
    26 changes: 26 additions & 0 deletions output-into-javascript.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    <?php

    $name = "David \"Dave\" O'Connor";

    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>PHP Data in JavaScript</title>
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    </head>
    <body>

    <h1>Demo</h1>

    <script>

    var name = <?= json_encode($name) ?>;

    alert('Hello ' + name);

    </script>

    </body>
    </html>
    23 changes: 23 additions & 0 deletions pass-using-ajax.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>PHP Data in JavaScript</title>
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    </head>
    <body>
    <h1>Demo</h1>
    <script>
    fetch('data.php')
    .then(function(response){
    return response.json();
    })
    .then(function(data){
    alert('Hello ' + data);
    });
    </script>
    </body>
    </html>
    29 changes: 29 additions & 0 deletions pass-using-cookie.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php

    $name = "David \"Dave\" O'Connor";

    setcookie("name", $name);

    ?><!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>PHP Data in JavaScript</title>
    <link rel="icon" href="data:;base64,iVBORw0KGgo=">
    </head>
    <body>

    <h1>Demo</h1>

    <script>

    var match = document.cookie.match(new RegExp('name=([^;]+)'));

    var name = decodeURIComponent(match[1]);

    alert('Hello ' + name);

    </script>

    </body>
    </html>