Page 1 of 1
Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 11:46 am
by bradbury
I am currently working on a calendar application that I made using javascript but need to get some values from a MYSQL database so I used PHP to do all that business. So my question was since PHP is a server side scripting language and javascript is a client side scripting language, when PHP parses the Javascript is the code read through my server instead of using the client to do some of the processing?
I could be completely wrong about all of this I was just curious and the only way to learn is to ask questions
Re: Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 11:52 am
by AbraCadaver
If your page is PHP then it will be parsed for PHP and the result sent to the browser where the JS will be interpreted. So given this:
Code: Select all
<?php
$message = 'Welcome!';
?>
<script type="text/javascript">
alert('<?php echo $message; ?>');
</script>
Then this will be sent to the browser:
[text]<script type="text/javascript">
alert('Welcome!');
</script>[/text]
HTH
Re: Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 12:03 pm
by bradbury
But if I am calling the 'javascript' like this:
Code: Select all
<script language="JavaScript" src="js/calendar.php"></script>
and all the javascript lines are called using:
Code: Select all
echo "function(){
//whatever code I have written
";
$con(mysql_connect(.....);
// more php then back to javascript
echo "function(){
.......
With this is the php module doing all the work on my server or is the client getting some of the work load?
Re: Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 12:14 pm
by AbraCadaver
Anything in PHP tags will be parsed and executed by PHP (db queries, echos etc.) and the result passed to the browser. At that point whatever pure javascript code is there will be executed in the browser.
Re: Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 12:41 pm
by bradbury
Oh kk. Well that makes sense then. I was just thinking about how if the php parsed the javascript and actually executed it then sent it to the browser how that might be a bit of an issue. But if php parses it and then sends it to the browser once it is parsed then there shouldnt be any major issues
Re: Parsing Javascript Using PHP
Posted: Tue Aug 31, 2010 4:41 pm
by pickle
PHP doesn't parse Javascript or any language other than PHP.
When you have a file with a .php extension, the PHP interpreter looks through it for "<?php" (or "<?" if shorttags are on). Anything between <?php and ?> is treated as PHP code. Period. If Javascript is in there, it had better be in a string, or the PHP interpreter is not going to know what to do with it.
As far as the PHP interpreter is concerned, in any given file there is PHP code and ... everything else. Only PHP code is parsed. Everything else is just passed through as-is.