somedude Posted January 13, 2007 Share Posted January 13, 2007 IS there a way to loop through a php array in a javascript loop<?php $name=array("hi","bye","yo"); ?><script language="JavaScript">var xx=new Array()var i=0for (i=0;i<=2;i++){xx="<?php echo $name["i"]; ?>"; alert(xx);} //}</script> Something like that, but obviously, i'm getting errors." Cannot use [] for reading in " Link to comment https://forums.phpfreaks.com/topic/33986-loop-php-array-in-javascript/ Share on other sites More sharing options...
trq Posted January 13, 2007 Share Posted January 13, 2007 No. PHP runs on the server while Javascript runs on the client.What you could do though is put all the elements you want into a javascript array. Something like...[code]<?php $name = array("hi","bye","yo"); ?><script language="JavaScript"> var arr = new Array("<?php echo implode('","',$name) ?>"); var i = 0; for (i=0; i<=2; i++) { alert(arr[i]); }</script>[/code] Link to comment https://forums.phpfreaks.com/topic/33986-loop-php-array-in-javascript/#findComment-159728 Share on other sites More sharing options...
somedude Posted January 13, 2007 Author Share Posted January 13, 2007 Thank you! Link to comment https://forums.phpfreaks.com/topic/33986-loop-php-array-in-javascript/#findComment-159740 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.