![]() |
Return array to AJAX - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Return array to AJAX (/showthread.php?tid=7382) |
Return array to AJAX - El Forum - 04-06-2008 [eluser]iamdesign[/eluser] Hey there, I was wondering how I could return an arry to an resulthandler of AJAX. I'm using jQuery and this is my code Code: function getGroupsBySubtypeId($id){ this returns the following data Code: Array Now i need this to fill up a select-box, but i can't find a way of doing it ![]() somebody who knows how to deal with this problem ![]() thnx in advance, cheers Return array to AJAX - El Forum - 04-06-2008 [eluser]Derek Allard[/eluser] yeah, CI returns a multi-dimensional array, and that's not super helpful. There are 2 ways to approach this, but I'd try this one first. Set your model up as Code: function getGroupsBySubtypeId($id) now in your controller, it'd look like Code: function somepage() You may need to adapt some of it for your specific application, but I think the general idea is here. One thing I'm not really hitting is how to return this data using "AJAX". The reason is because there are so many things this might look like. Return array to AJAX - El Forum - 04-06-2008 [eluser]richthegeek[/eluser] well AJAX implies XML - therefore changing that structure into XML using this function: Code: function array_to_xml( $input, $level) { Then with jQuery you can do (remember to return as Content-type:text/xml): Code: function( data ) { Return array to AJAX - El Forum - 04-07-2008 [eluser]gon[/eluser] You should convert the array to JSON, and send it to the browser. Then just evaluate it and you get the data as a javascript object, so you can populate the select. And you can change the array structure before converting it to JSON, to make the JSON data easier to use with javascript. Cheers. Return array to AJAX - El Forum - 04-07-2008 [eluser]iamdesign[/eluser] ow thanks alot! great info i just got into ajax through jquery so i didn't know all the possibilities :-) tnx for the help! |