Vars from PHP to JS and Back

09Jul08

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>

Advertisements


One Response to “Vars from PHP to JS and Back”

  1. It is a good example. Easy to understand and flexible.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: