• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 537
  • Last Modified:

PHP to return an array to SAJAX, having trouble

Hi,
I have sajax working, im new to it.

I have a PHP function:

function get_table2_data(){
      return array('achievements', 'second');

}

then my javascript function that handles the data does this:

function do_table2_update_cb(table_arr) {
      alert(table_arr[0]);
      for (j = 0; j < table_arr.length; j++)
      {
              var table2_el = document.getElementById('table2');
              table2_el.options[table2_el.options.length] = new Option(table_arr[j], table_arr[j]);
      }
}

and that first alert is just showing 'A' as if table_arr is just a string with a value of 'Array'

Any ideas how to get PHP to return a javascript array

Thanks
Ant
0
antum
Asked:
antum
1 Solution
 
malaiacCommented:
helo,

try this:

function get_table2_data(){
     return implode(",",array('achievements', 'second'));
}


function do_table2_update_cb(table_arr) {
     //table_arr is now a csv string
     alert(table_arr);
     //now we do an explode after comma
     var arrSplitString=table_arr.split(",");

     for (j = 0; j < arrSplitString.length; j++)
     {
            var table2_el = document.getElementById('table2');
            table2_el.options[table2_el.options.length] = new Option(arrSplitString[j], arrSplitString[j]);
     }
}

in this way you do not send arrays, you send strings

hope it helped
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now