Solution: Javascript with PHP

They have: 105 posts

Joined: Jan 2002

I know that a lot of people come here looking for answers. Here's one I couldn't find a solution to on the forum:

Question :
How do I insert javascript in php code?

Answer :
You just echo / print the javascript like you would do with html, like:

<?
echo "<script language=\"JavaScript\">\n";
echo "alert("javascript from php");\n";
echo "</script>";
?>

CPRhosting.com - CPR Hosting...Giving life to the web
Custom-made Hosting Plans starting at $2

They have: 8 posts

Joined: May 2002

The other way is just as fun -- getting PHP outputs to come up in JScript:

&lt;script language="JScript"&gt;
    window.alert( "You have this much 1337ness: " + <?php echo $result; ?> );
&lt;/script&gt;
'

Just make sure it's in a file that's parseable by PHP (.php/.php3/.phtml etc).

arin

[edit]: I can't spell 1337ness Sad

Web hosting from just $2 a month! http://www.xeosdd.net

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

1337|\|355 Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.