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 Quote 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. Quote 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? Quote 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 Quote 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> Quote 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. Quote 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 Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.