Java Script and PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.



Re: Java Script and PHP

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



echo '<script type="text/javascript">alert("javascript alert")</


Re: Java Script and PHP

macca wrote:
Quoted text here. Click to load it

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"  

-- :: virtual tour

Re: Java Script and PHP

Quoted text here. Click to load it

This will be send with the wrong Header (normally text/html). You have  
to add
header('Content-type: application/x-javascript');

Re: Java Script and PHP

Quoted text here. Click to load it

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


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

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

Re: Java Script and PHP

Quoted text here. Click to load it


<title>Alternative PHP usage</title>
<script language="javascript">
<?php if($variable!=true){?>
      alert("variable is false!");
        echo "</script>";
        echo "</body>";
        echo "</html>";
      else {?>
      alert("Ok variable is true");
      <?php }?>

I always use this kind of stuff in validating forms and session

Site Timeline