Well, it depends on what you are really wanting to do.
If you want to have javascript that is generated from PHP, then the sample you gave is fine, just needs to be that php echos out proper javascript in the block:
Code: Select all
<script type="text/javascript">
<?php
echo ' var testVar="' . addslashes($variableFromDB) .'";';
?>
alert(testVar); // WIll be whatever was in $variableFromDB
</script>
Keep in mind that this, as far as the executing of javascript is concerned, is static. that is to say that is $variableFromDB was set to
Greg, then the code as seen (and used) by the browser will be:
Code: Select all
<script type="text/javascript">
var testVar="Greg";
alert(testVar);
</script>
PHP is executed Server Side (sending its output to the browser). Javascript is executed Client Side (does everything with the only the code provided to the browser. either directly or through SRC= attributes)
To have PHP code execute based upon something that javascript is doing, you have to have the web page make a call back to the server to a PHP script, and then process the output that gets sent back. Haven't done it enough with jquery to give an example offhand, but in the documentation, do a search for AJAX.
-Greg