Vars from PHP to JS and Back

The easy part is to add a js variable from php.
<html>
<?php
$MyVar3 = “Something in PHP”;
?>
<script language=”JavaScript”>
alert(<?php print “‘”.$MyVar3.”‘”; ?>);
</script>
</html>

The hard part is to take values from Javascript to PHP

<html>
<script language=”JavaScript”>
var JSVar = ‘something from JS code’;
</script>
<?php

$MyVar1 = “?><script language=javascript>document.write(‘Something you want’);</script><?php”;
$MyVar1 = str_replace(“?>”, “”, $MyVar1);
print $MyVar1.”<br><br>”;

$MyVar2 = “?><script language=javascript>document.write(JSVar);</script><?php”;
$MyVar2 = str_replace(“?>”, “”, $MyVar2);
print $MyVar2;
?>
</html>

Leave a Reply

Your email address will not be published.