Jump to content

Possible to pass PHP array to JS function?


ninedoors

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.