AJAX AND JSON - 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: AJAX AND JSON (/showthread.php?tid=25309) |
AJAX AND JSON - El Forum - 12-07-2009 [eluser]sico87[/eluser] Hello, I have a website where I am wanting to get some query results through AJAX and JSON, but I have absolutly no idea how to do it, I have this function, Code: public function category() { Essintially the query that is run is dynamic depending on what url is being passed, what I need to is, for the user clicks a link something like Code: <a href="/category/blog/1" id="blog">Read the blog</a> From this link I get blog and 1 passed to the query, but I need to load the results in a view that is loaded in to my main template and then everytime a link is clicked do the same thing without overwriting the previous data, does anyone have any idea how to do this? AJAX AND JSON - El Forum - 12-07-2009 [eluser]Ben Edmunds[/eluser] If you are using jQuery look up the ajax and post functions. If not look up Javascript XmlHttpRequest. AJAX AND JSON - El Forum - 12-08-2009 [eluser]Jamie Rumbelow[/eluser] You'll also need to check that the request is being made through AJAX - create an AJAX helper (create a applications/helper/ajax_helper.php file) and put this code in it: Code: function is_ajax() { Then in your controller you can use that function to check if it's an AJAX request, rendering the JSON response if it is and the entire HTML page if it isn't. If you want a more robust API, have a look at Phil Sturgeon's REST library to implement a full API that works nice with JavaScript and CodeIgniter Jamie AJAX AND JSON - El Forum - 12-08-2009 [eluser]Krzemo[/eluser] Some code below from top of my head (no warranty though ) Code: //in da controller and the rest is done by javascript (me like jQuery) Code: //this is to be used with an event AJAX AND JSON - El Forum - 12-17-2009 [eluser]asylmottaket[/eluser] Here is what im using in the controller: Code: if($this->input->post('submit')) I also use jQuery, and with the jQuery form plugin, ajax form submission is quite easy |