Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is nothing happening when I try to run this JS code inside PHP?

I have a file named test.php which has this whole code :

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Payment Receipt</title>
</head>
<body>

<?php
...
$result = $stmt->get_result();
$row = $result->fetch_assoc();

if($row) {
    $obj->txndate = $row['date'];
    $obj->txnid = $row['txnid'];
    $obj->atomid = $row['atomid'];
    $obj->amount = $row['amount'];
    
    $myJSON = json_encode($obj);
    echo $myJSON;

    //javascript starts inside PHP

    echo '<script>', PHP_EOL;
    echo 'var msg = <?php echo json_encode($myJSON); ?>;', PHP_EOL;
    echo 'var ThunkableWebviewerExtension = {', PHP_EOL;
    echo 'postMessage: function (message) {', PHP_EOL;
    echo 'if (window.ReactNativeWebView) {', PHP_EOL;
    echo 'window.ReactNativeWebView.postMessage(message);', PHP_EOL;
    echo '} else {', PHP_EOL;
    echo 'window.parent.postMessage(message, '*');', PHP_EOL;
    echo '}', PHP_EOL;
    echo '}', PHP_EOL;
    echo '}', PHP_EOL;
    echo 'ThunkableWebviewerExtension.postMessage(msg);', PHP_EOL;
    echo '</script>', PHP_EOL;
    
} else {
    echo 'Incorrect ID';
}

$conn->close();

?>

</body>
</html>

When I run this code, nothing happens in the JavaScript side - everything works well until the JavaScript starts. This is the JavaScript code I tried to embed in the PHP code :

<!DOCTYPE html>
<html>
<body>

<?php
$myJSON = 'Hi there!';
?>

<script>
var msg = <?php echo json_encode($myJSON); ?>;
var ThunkableWebviewerExtension = {
    postMessage: function (message) {
        if (window.ReactNativeWebView) {
            window.ReactNativeWebView.postMessage(message);
        } else {
            window.parent.postMessage(message, '*');
        }
    }
}

ThunkableWebviewerExtension.postMessage(msg);
</script>

</body>
</html>

The above code works well as expected when I run it separately in a different file, which too has the PHP variable passed in the var msg command.

I wonder what I did wrong trying to write JavaScript code inside PHP - I saw somewhere that by echoing the statement, then adding PHP_EOL at the end we can write JS/HTML in-between PHP.

What's wrong here? Any help would be appreciated!

like image 825
BigSantuary Avatar asked Dec 09 '25 02:12

BigSantuary


1 Answers

This does not work:

echo '<script>', PHP_EOL;
echo 'var msg = <?php echo json_encode($myJSON); ?>;', PHP_EOL;

because it sends PHP code to the client, which will not execute it since it has no PHP parser.

Write it like this:

$jsonEncoded = json_encode($myJSON);

echo <<<JS001
<script>
var msg = {$jsonEncoded};
var ThunkableWebviewerExtension = {
    postMessage: function (message) {
        if (window.ReactNativeWebView) {
            window.ReactNativeWebView.postMessage(message);
        } else {
            window.parent.postMessage(message, '*');'
        }
    }
};
ThunkableWebviewerExtension.postMessage(msg);
</script>
JS001;
// The line above must end in ";" (not even spaces after) and the J must start
// on the very first columns.

Other sections in the same file would be called JS002, and so on. Inside each "here-document" section, if you want to embed a php variable write it like {$variable}, and if you have a $ sign otherwise, escape it with a backslash, \$.

like image 125
LSerni Avatar answered Dec 11 '25 16:12

LSerni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!