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. 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)?> ); 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? 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 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>"; ?> 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 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 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>'); } 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 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"? 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. 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 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. 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> 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 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
Archived
This topic is now archived and is closed to further replies.