Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: php variable to javascript function


Message #1 by "arun" <arun@l...> on Wed, 10 Apr 2002 18:19:07 +0530
> Can any one help me in passing a php variable to a javascript function.

Generating javascript is no different than generating HTML -- they're both
just text that you send from the server to the client.

In HTML, you'd use a php variable to, say, populate a form text field:

echo "<INPUT type=\"text\" name=\"username\" value=\"{$username}\">;

Nothing new there.  (I hope!)  How could generating javascript be any
different?


> I want to know the syntex...Below is my code..

> 	document.write('<td align="'+alignment+'"
> bgcolor="'+backgroundcolor+'"
> colspan="'+colspan+'" height="10"><a href="topframe.php?index="<? echo
> $frameno ?>><img src="'+imgsrc+'"></a></td>');

Okay -- the only PHP code in there is the <? echo $frameno ?> part.

That's all you need.  The way you have it written, alignment,
backgroundcolor, colspan, and imgsrc are all javascript variables, not PHP
vars.


I did notice, though, that you close your href attribute early.

You have this:

<a href="topframe.php?index="<? echo $frameno ?>>

What you probably want is this:

<a href="topframe.php?index=<? echo $frameno ?>">


see the difference?  Your problem wasn't generating javascript, it was
generating HTML.


Nik


  Return to Index