Ninjakreborn Posted June 25, 2008 Share Posted June 25, 2008 I have done this before without a problem. I need to take an array within PHP and pass it into Javascript. I am using EXTJS library..with it's filter grids. About 3 of my "fields" need a list of selectable items from filtering and those options are dynamically passed via PHP into javascript. As far as I was aware there are 2 ways to do this and I have done both successfully so far on multiple occassions. 1. Get the array ready in PHP. Turn it into JSON and send it to the page. Then grab the JSON string with javascript and decode it. 2. Just simply pass the array into Javascript. I have done both methods but the following code is returning strange results: var subarealist = '{{$subareas}}'; var x; var subareastring = ''; for (x in subarealist) { alert(subarealist[x]); subareastring += subarealist[x] + ', '; } That is just a snippet from my overall code. (I am using a modified version of smarty for this project so you'll notice {{$variable}} instead of {$variable}. In this it's suppos to * Create subarealist var which has the array put into it (I have done it this way before). * Create an empty x variable which will be auto-incrementing in the for/in loop. * Create subareastring variable that's empty to store the final string. * Look through array and grab the data and put it into the string in the required format. After that I try alerting subareastring...instead of returning what I need it ends up returning half written function calls INSIDE of that variable. Also if I alert each time the for/in loop goes through like alert(subarealist[x]) it ends up just alerting parts of function calls (one's I have never seen on top of that). IT's very strange behaviour and any advice would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
lemmin Posted June 25, 2008 Share Posted June 25, 2008 Why don't you just do something like this? <script language="Javascript"> var javaArray = new Array( <?php $phpArray = array(1,2,3,4,5); echo implode(",", $phpArray); ?> ); </script> Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted June 30, 2008 Author Share Posted June 30, 2008 I should have..I would have. That's the problem. I was having a minute of absent minded-ness. After fighting with i awhile I remembered how easy it really was. Quote Link to comment 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.