include php within javascript

thezone1

New Member
Messages
192
Reaction score
0
Points
0
Hi i have just setup prototype window class,
but no i need to display php output in the windows,
( i want to include php in javascript.js )

so does anyone know how i can do this..

thanks
 

supajason

Member
Messages
288
Reaction score
2
Points
18
this is an example:

main page:
PHP:
<style type="text/css" media="screen">
  <?php include('./includes/layout.php'); ?></style>

layout.php:
PHP:
body,td,th {
	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:12px;
}
<?PHP echo "hello"; ?>

off the top of my head anyway?
 
Last edited:

thezone1

New Member
Messages
192
Reaction score
0
Points
0
sorry i did try it, but nothing i think its because im running it in a .js file and not .php so anymore ideas guys ??
 

FireLancer

New Member
Messages
15
Reaction score
0
Points
0
Php is run ont he server, all php code is then removed and the reamining page sent to the client
Java Script is not ran on the server but just sent to the client. This means that:

php can generate java script (because php is run first)
javascript cannot generate php as it has already left the server.
eg this works:
Code:
</php
 echo
   "<script type=\"text/javascript\">\n".
   " document.write(\"Hello World!\");\n".
   "</script>\n";
?>
but this doesn't
Code:
<script type="text/javascript">
document.write("<?php echo \"Hello World\"; ?>");
</script>

I don't think the server parses .js files for php etc. Try making it a .php file and use the php header() function to set the content type to text/javascript. I think it's header("content-type: text/javascript");
 
Last edited:

supajason

Member
Messages
288
Reaction score
2
Points
18
you need to do this:

add this to the main page:
PHP:
<script type="text/javascript" src="myjsfile.php"></script>

rename your js file to myjsfile.php, then in your new php(js) file you put:

PHP:
<?PHP
Header("content-type: application/x-javascript");
$serverIP=$_SERVER['REMOTE_ADDR'];
echo "document.write(\"Your IP address is: <b>" . $serverIP . "</b>\")";
?>
 
Last edited:

thezone1

New Member
Messages
192
Reaction score
0
Points
0
Thanks for the help supajason, you out me on the right path, turn's out the the site runs off the index so i had to link to it there.
 
Top