PHP to return an array to SAJAX, having trouble

Posted on 2006-03-27
Last Modified: 2008-01-09
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) {
      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

Question by:antum
    1 Comment
    LVL 3

    Accepted Solution


    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
         //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

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Wordpress New Install 3 48
    substitute for $_SESSION? 4 31
    Joomla main HTML page 4 19
    posting form data 3 0
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now