ninedoors Posted October 1, 2008 Share Posted October 1, 2008 How would I go about passing a php array that I create from a database result to a JavaScript function that takes an array as an argument? This is what I have but clearly it is not working properly. For example, have an array that is: <?php $vals = array(1,2,3,4,5,6,7,8,9,10,11,14,15); ?> And then I wan to pass it to this JS function like: <?php echo "<script type=\"text/javascript\"> put_picker(\"$vals\");</script>"; ?> The JS function is a table with numbers in it that when you click on a number it writes that number to the textbox that the page is focussed on. Here is the function: function put_picker(vals){ document.write('<div id="container" class="picker_container" style="width:239px">'); for (ev in vals){ document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>\r\n '); } document.write('</div>'); } Note that when I do this the JS function works so it must be how I am passing the php array to it. function put_picker(){ var vals = [1,2,3,4,5,6,7,8, 9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24, 25,26,27,28,29,30,33,40,41, 44,46,51,54,55,66,68,69, 73,77,83,87,88,91,93,97,]; document.write('<div id="container" class="picker_container" style="width:239px">'); for (ev in vals){ document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>\r\n '); } document.write('</div>'); } Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/ Share on other sites More sharing options...
Barand Posted October 1, 2008 Share Posted October 1, 2008 var vals = new array(<?php echo join(',' , $vals)?> ); Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654943 Share on other sites More sharing options...
ninedoors Posted October 1, 2008 Author Share Posted October 1, 2008 Ok, where would that go? Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654949 Share on other sites More sharing options...
DeanWhitehouse Posted October 1, 2008 Share Posted October 1, 2008 My idea would be doing something like <script> var Arr = new array ( <?php $value = array('1','2','3'); //etc foreach($value as $val) { echo "'".$val"',"; } ?> ); </script> Edit: Beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654951 Share on other sites More sharing options...
Barand Posted October 1, 2008 Share Posted October 1, 2008 <?php echo "<script type=\"text/javascript\"> var vals = new array(" . join(',' , $vals) . "); put_picker(vals); </script>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654956 Share on other sites More sharing options...
DeanWhitehouse Posted October 1, 2008 Share Posted October 1, 2008 @barrand <?php echo " <script type=\"text/javascript\"> var vals = new array(".join(',' , $vals)." ); put_picker(vals); </script>"; ?> Beat me to it again, lolz Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654959 Share on other sites More sharing options...
Barand Posted October 1, 2008 Share Posted October 1, 2008 already spotted and edited Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654961 Share on other sites More sharing options...
ninedoors Posted October 1, 2008 Author Share Posted October 1, 2008 This is what I have and I am seeing nothing ouputed on the page. <?php "<script type=\"text/javascript\"> var vals = new array(".join(',' , $vals)."); put_picker(vals); </script>"; ?> And my function is: function put_picker(vals){ document.write('<div id="container" class="picker_container" style="width:239px">'); for (ev in vals){ document.write('<div class=box onclick="s(this)">'+vals[ev]+'</div>\r\n '); } document.write('</div>'); } Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654963 Share on other sites More sharing options...
DeanWhitehouse Posted October 1, 2008 Share Posted October 1, 2008 try mine Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654965 Share on other sites More sharing options...
Barand Posted October 1, 2008 Share Posted October 1, 2008 What happened to "echo"? Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654968 Share on other sites More sharing options...
ninedoors Posted October 1, 2008 Author Share Posted October 1, 2008 I know this is a JS question but is there something different I have to do in the function because it is an array and not a variable? Yes I saw that and added the echo but still nothing. Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654970 Share on other sites More sharing options...
DeanWhitehouse Posted October 1, 2008 Share Posted October 1, 2008 where is $vals ? also view the source and see if the javascript is there properly Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654972 Share on other sites More sharing options...
ninedoors Posted October 1, 2008 Author Share Posted October 1, 2008 Ok Blade, I tried yours but still nothing. I'll check the source now. Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654974 Share on other sites More sharing options...
ninedoors Posted October 1, 2008 Author Share Posted October 1, 2008 This is what Barand's looks like: <td><script type="text/javascript"> var vals = new array(1,2,4,3,5,6,7,13,19,22); put_picker(vals); </script></td> And Blade's: <td> <script> var Arr = new array ( '1','2','4','3','5','6','7','13','19','22',); put_picker(Arr); </script> </td> Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654978 Share on other sites More sharing options...
DeanWhitehouse Posted October 1, 2008 Share Posted October 1, 2008 Ok, then i believe it is your JS as the php is working Quote Link to comment https://forums.phpfreaks.com/topic/126652-possible-to-pass-php-array-to-js-function/#findComment-654987 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.