|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
how to pass array from php to javascriptHere is my sample code: <?php require "include/connection.php"; //$menu_name=$_GET['menus']; //$menulist= array("Peter", "Joe", "Glenn", "Cleveland"); //reset($menulist); $menulist=array(); $q="select name from menus"; $result=mysql_query($q); while($row=mysql_fetch_array($result)){ $name=$row['name']; array_push($menulist,'$name'); } ?> <script type="text/javascript"> var menu1=new Array() .............//I want to copy array menulist to menu1,how to program here?I want the menu1 to be the same as menulist </script> See how Windows Mobile brings your life together—at home, work, or on the go. See Now _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php |
|
|
Re: how to pass array from php to javascriptOn Sat, Sep 13, 2008 at 5:42 PM, chad qian <nynj.tech@...> wrote:
Hello, you could use json_encode() to encode your array in php to the json format and then print it out from php inside the html, then decode the array using js's json functions. But as long as you do that you could print it straightforward, just write from php the "html" code that defines the array in js. _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php |
|
|
Re: how to pass array from php to javascriptOn Sat, Sep 13, 2008 at 12:10 PM, Cristian Baltatescu
<cbaltatescu@...> wrote: > > > On Sat, Sep 13, 2008 at 5:42 PM, chad qian <nynj.tech@...> wrote: >> >> How to pass array from php to javascrip?thanks! >> >> Here is my sample code: >> <?php >> require "include/connection.php"; >> //$menu_name=$_GET['menus']; >> //$menulist= array("Peter", "Joe", "Glenn", "Cleveland"); >> //reset($menulist); >> $menulist=array(); >> $q="select name from menus"; >> $result=mysql_query($q); >> while($row=mysql_fetch_array($result)){ >> $name=$row['name']; >> array_push($menulist,'$name'); >> } >> ?> >> <script type="text/javascript"> >> var menu1=new Array() >> .............//I want to copy array menulist to menu1,how to program >> here?I want the menu1 to be the same as menulist >> </script> >> > > Hello, > you could use json_encode() to encode your array in php to the json format > and then print it out from php inside the html, then decode the array using > js's json functions. > But as long as you do that you could print it straightforward, just write > from php the "html" code that defines the array in js. +1 for json... below is what you need to do: <script type="text/javascript"> var menu1= eval( '(' + <?php echo json_encode($menulist); ?> ')' ); </script> -John C. _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php |
|
|
Re: how to pass array from php to javascriptOn Mon, Sep 15, 2008 at 12:13 AM, John Campbell <jcampbell1@...> wrote:
> On Sat, Sep 13, 2008 at 12:10 PM, Cristian Baltatescu > <cbaltatescu@...> wrote: >> >> >> On Sat, Sep 13, 2008 at 5:42 PM, chad qian <nynj.tech@...> wrote: >>> >>> How to pass array from php to javascrip?thanks! >>> >>> Here is my sample code: >>> <?php >>> require "include/connection.php"; >>> //$menu_name=$_GET['menus']; >>> //$menulist= array("Peter", "Joe", "Glenn", "Cleveland"); >>> //reset($menulist); >>> $menulist=array(); >>> $q="select name from menus"; >>> $result=mysql_query($q); >>> while($row=mysql_fetch_array($result)){ >>> $name=$row['name']; >>> array_push($menulist,'$name'); >>> } >>> ?> >>> <script type="text/javascript"> >>> var menu1=new Array() >>> .............//I want to copy array menulist to menu1,how to program >>> here?I want the menu1 to be the same as menulist >>> </script> >>> >> >> Hello, >> you could use json_encode() to encode your array in php to the json format >> and then print it out from php inside the html, then decode the array using >> js's json functions. >> But as long as you do that you could print it straightforward, just write >> from php the "html" code that defines the array in js. > > +1 for json... below is what you need to do: > > <script type="text/javascript"> > var menu1= eval( '(' + <?php echo json_encode($menulist); ?> ')' ); > </script> Whoops.... it's a bit late. You don't even need to bother with the eval stuff. using eval becomes important when you are using the xmlHTTPRequest. just: <script> var menu1 = <?php echo json_encode($menulist); ?>; </script> Regards, John Campbell _______________________________________________ New York PHP Community Talk Mailing List http://lists.nyphp.org/mailman/listinfo/talk NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php |
| Free embeddable forum powered by Nabble | Forum Help |