Forum Moderators: coopster

Message Too Old, No Replies

Running PHP code from javascript

         

Jacob or JaF

11:23 pm on Sep 20, 2006 (gmt 0)

10+ Year Member



How can I run PHP code from javascript?

eelixduppy

11:24 pm on Sep 20, 2006 (gmt 0)



You can't for the most part(javascript=client-side, PHP=server-side..there must be some link). What are you trying to do? Maybe there is an alternative.

dreamcatcher

1:14 pm on Sep 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to change your javascript using PHP, you can load the js content on your .php page and change the data dynamically. But, yep, what are you trying to do?

dc

spinnercee

1:17 pm on Sep 21, 2006 (gmt 0)

10+ Year Member



You can (call a PHP script in a JavaScript "script" tag) , as long as the PHP script returns JavaScript code.

One easy method/use is for inserting/embedding text or HTML -- the PHP would just return a JavaScript document.write() command.


// You may need to add a content-type header:
header("Content-type: application/x-javascript");
// This must be done before the script outputs any data.

$output_string="Hello There<br>";

echo "document.write('" . $output_string . "');";

/*
The script would be called within a <script src="script.php"></script> object and it can return any JavaScript code you like.
*/

You can also return images, and other dynamic content this way --

[edited by: coopster at 1:48 pm (utc) on Sep. 21, 2006]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]