'How to alert PHP value in JavaScript?

<script type="text/javascript">
    var jvalue = 'this is javascript value';
    <?php
    $abc = "<script>document.write(jvalue)</script>" ?>
</script>

<?php echo 'php_'.$abc; ?>

<script type="text/javascript">
    var test = "<?php echo $abc; ?>";
</script>

<?php
    echo '<script language="javascript">';
    echo 'alert(test)';
    echo '</script>';
?>

How to alert test variable, which contains PHP value? I would like to get the PHP value in javascript, for further execution in my project.



Solution 1:[1]

try this code

<?php
    echo '<script language="javascript">alert("'.$test.'"); </script>';
?>

this is updated code for you using javascript and PHP

<?php 
   $user = "rebel";

   echo '<script> var name = "'.$user.'";
   alert(name);</script>';
?>

This is the third code for you if this does not work then you have other problem

<?php 
    $abc= "Hello";
?>
<script type="text/javascript">
    var test="<?php echo $abc; ?>";
</script>

<?php
    echo '<script language="javascript">';
    echo 'alert(test)';
    echo '</script>';
?>

Solution 2:[2]

why are you assigning the variable to a javascript variable and then echoing that? sounds like extra work to me...

echo 'alert("' . $abc . '");';

Solution 3:[3]

Your code is not so clear, i think you want something like this:

<?php
    $test = "hi there!";
    echo '<script type="text/javascript">';
        echo 'alert("'.$test.'")';
    echo '</script>';
?>

Solution 4:[4]

var test="<?php echo $abc; ?>"
    <script language="javascript">alert(test); </script>

Solution 5:[5]

If you want to alert the value of $abc, you need to escape the slash in the following line with a backslash, like this.

<?php $abc = "<script>document.write(jvalue)<\/script>" ?>

You also need to remove this line (or escape the < and the > in $abc). If you don't, the script tags will mess up the rendered html code.

<?php echo  'php_'.$abc; ?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 xxx
Solution 2 serakfalcon
Solution 3 iubema
Solution 4
Solution 5