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) Link to comment https://forums.phpfreaks.com/topic/157088-solved-mixing-php-in-js/ 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. Link to comment https://forums.phpfreaks.com/topic/157088-solved-mixing-php-in-js/#findComment-827458 Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 Great thanks! Link to comment https://forums.phpfreaks.com/topic/157088-solved-mixing-php-in-js/#findComment-827459 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. Link to comment https://forums.phpfreaks.com/topic/157088-solved-mixing-php-in-js/#findComment-827462 Share on other sites More sharing options...
galvin Posted May 6, 2009 Author Share Posted May 6, 2009 Awesome, thanks a lot Demonic!!! Link to comment https://forums.phpfreaks.com/topic/157088-solved-mixing-php-in-js/#findComment-827464 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.