view php msg in alert javascript [duplicate]

The error you see is a JS error, which means that either there’s a problem with your JS code (perhaps some other part of the front end code you’re not showing us is flawed). Alternatively, the PHP value is causing problems. The way to easily insert PHP values in JS is through JSON:

var tmp = <?= json_encode($someVar); ?>;
//<?= is short for <?php echo
alert(tmp);

That shouldn’t cause any trouble, no matter what the value of $someVar is.
With your updated code snippet, the issue is that the value of $test is not being quoted when passed to the alert function. Change it to this, and it’ll work:

<?php $test="test";?>
<script> alert(<?= json_encode($test); ?>); </script>

Browse More Popular Posts

Leave a Comment