By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,377 Members | 3,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,377 IT Pros & Developers. It's quick & easy.

Java Script and PHP

P: n/a
I have an application that uses Google Maps. This works fine but I ned
to read a file. I can do this in PHP but how to mix PHP with Java
Script? Do they live together or do I need to end each block of script
then start the php - then end it etc.

regards
Hardy
Dec 22 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
PHP is server side and is converted to HTML by the time it reaches the
client (browser). JavaScript is client side and is interpreted by the
client (browser) on the other side.

Thus, you can write javascript with PHP but you cant write PHP with
javascript.

e.g.

<?php

echo '<script type="text/javascript">alert("javascript alert")</
script>';
?>
Dec 22 '07 #2

P: n/a
macca wrote:
PHP is server side and is converted to HTML by the time it reaches the
client (browser). JavaScript is client side and is interpreted by the
client (browser) on the other side.

Thus, you can write javascript with PHP but you cant write PHP with
javascript.

e.g.

<?php

echo '<script type="text/javascript">alert("javascript alert")</
script>';
?>
Or save a file named myscript.js.php. In the file you can use both
javascript and php. For example, you can write:

alert( <?php 12 + 15 ?)

and then include it in your html with:

<script type="text/javascript" language="JavaScript"
src="myscript.js.php"></script>

--
www.panoye.com :: virtual tour
Dec 23 '07 #3

P: n/a
Or save a file named myscript.js.php. In the file you can use both
javascript and php. For example, you can write:

alert( <?php 12 + 15 ?)

and then include it in your html with:

<script type="text/javascript" language="JavaScript"
src="myscript.js.php"></script>

--
www.panoye.com :: virtual tour
This will be send with the wrong Header (normally text/html). You have
to add
header('Content-type: application/x-javascript');
Dec 23 '07 #4

P: n/a
On Dec 23, 1:43 pm, Jonas Werres <jo...@example.orgwrote:
Or save a file named myscript.js.php. In the file you can use both
javascript and php. For example, you can write:
alert( <?php 12 + 15 ?)
and then include it in your html with:
<script type="text/javascript" language="JavaScript"
src="myscript.js.php"></script>

Its still the same thing. The PHP is interpreted on the server before
the javascript on the client,

so, what is sent to the client is actually

alert(27)

it's the same as having a file called script.php and writing

<script language="javascript" type="text/javascript">
alert(<?php echo 12 + 15; ?>)
</script>

Dec 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.