Re: PHP var into javascript var

From: Date: Sat, 28 Oct 2000 09:02:10 +0000
Subject: Re: PHP var into javascript var
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-22429@lists.php.net to get a copy of this message
On Wed, 27 Sep 2000, LDL Enterprise wrote: > How can I put a $myrow[product] variable that is in the <body> to a > javascript var inside a function in the <head> Well, the thing to remember is that PHP is server-side, and JavaScript is client-side. The PHP code will be run by the server BEFORE it is sent to the client. I think the easiest way to do it would be to create a generic JavaScript function to pop open a window to a given picid: <head> <script language="JavaScript" type="text/javascript"> <!-- function PopWindow (picid) { window.open ("view_pic.php?picture_id=" + picid, "MyWindow", ... ); } </script> </head> And then when you're writing out your table and buttons, you should write out the product ID in the OnClick handler - so that when you view the source of the generated page, it looks like it's been hard-coded in, but it's actually written by the PHP script based on the data in the table. I hope I'm making some sort of sense. Some code-type stuff: <table> <?php while ($myrow = mysql_fetch_array($result)) { // print out table guff... print '<form>' . '<input type="button" value="' . htmlentities($myrow["product"]) . '"' . " onClick=\"PopWindow('" . $myrow["productid"] . "');\">"; // print more guff... } ?> </table> This would result in, say... <input type="button" value="Angry Beaver" onClick="PopWindow(5);"> The JavaScript function would then open up view_pic.php?picture_id=5 Hope this is what you wanted :-) -- Michael

« previous php.general (#22429) next »