galvin Posted May 6, 2009 Share Posted May 6, 2009 Just curious if this is possible (don't want to try and write the code if it's not even possible). My basic question is…Can you mix PHP inside Javascript? For example, I am going to have a database (mySQL) with list items in it and I'm going to query all the list items using PHP/MySQL and then I want to use the result of the query to populate a Javascript array. So for example, if my php/mysql query brings back 5 list items... monkey cat frog shark llama I’d like to then populate those values into a Javascript array. So Javascript wise, I’d start with an empty array like… var randomAnimals=new Array(); And then I’d want to take those list items retrieved from the database via php/mysql and somehow loop through them and populate them into the JS array and ultimately end up with… var randomAnimals=new Array(“monkey”, “cat”, “frog”, “shark”, “llama”); If it is possible, I think I should be able to write the code. Again, just want to make sure it's doable to essentially mix PHP in Javascript. (and sorry if this should be in the JS section, rather than PHP. I couldn’t decide so I put it in the PHP section) Quote Link to comment Share on other sites More sharing options...
Demonic Posted May 6, 2009 Share Posted May 6, 2009 Yes its possible, just echo the data out like normal. Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 Great thanks! Quote Link to comment Share on other sites More sharing options...
Demonic Posted May 6, 2009 Share Posted May 6, 2009 Just a note: echo "var randomAnimals=new Array(\"" . implode('"',$myarray) . "\");"; Would output what your expecting. Quote Link to comment Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 Awesome, thanks a lot Demonic!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.