james909 Posted July 18, 2013 Share Posted July 18, 2013 I am trying to declare javascript variables from php array values using a loop. Trying to declare javascript var h1 as php array value from $array[1][h], and h2 as $array[2][h], and h3 as $array[3][h],and so on... Here is my code: <SCRIPT LANGUAGE="JavaScript"> for (var i=0;i<61;i++) { var h[i] = "<?php echo $array[i][h] ?>"; var hl[i] = "<?php echo $array[i][hl] ?>"; var hs[i] = "<?php echo $array[i][hs] ?>"; } </script> When I <script type="text/javascript"> document.write(h1); </script> it is blank Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/ Share on other sites More sharing options...
trq Posted July 18, 2013 Share Posted July 18, 2013 PHP is evaluated on the server before your JavaScript even exists on the browser. Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441223 Share on other sites More sharing options...
james909 Posted July 18, 2013 Author Share Posted July 18, 2013 Thanks trq, so is there anyway to use the javascript loop increments number inside php variable value echo? Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441224 Share on other sites More sharing options...
james909 Posted July 18, 2013 Author Share Posted July 18, 2013 I can assign the variables fine like this: var h1 = "<?php echo $array[1][h] ?>", h2 = "<?php echo $array[2][h] ?>", h3 = "<?php echo $array[3][h] ?>", etc..... Trying to loop this Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441225 Share on other sites More sharing options...
trq Posted July 18, 2013 Share Posted July 18, 2013 No. You need to use php. <script> <?php for ($i=0; $i < 61; $i++) { echo "var h[{$i}] = '{$array[$i]['h']}';"; echo "var h1[{$i}] = '{$array[$i]['h1']}';"; echo "var hs[{$i}] = '{$array[$i]['hs']}';"; } ?> </script> Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441226 Share on other sites More sharing options...
trq Posted July 18, 2013 Share Posted July 18, 2013 ps: document.write is so 1995. Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441227 Share on other sites More sharing options...
james909 Posted July 18, 2013 Author Share Posted July 18, 2013 Thank you trq, code looks good I will try that. and I was just using document.write to see what the javascript var contained Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441228 Share on other sites More sharing options...
trq Posted July 18, 2013 Share Posted July 18, 2013 Thank you trq, code looks good I will try that. and I was just using document.write to see what the javascript var contained Thats what console.log() is for. Link to comment https://forums.phpfreaks.com/topic/280278-declaring-javascript-variables-in-a-lopp/#findComment-1441238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.