• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to implement ajax in Code igniter

#1
[eluser]rajyakshmik[/eluser]
Hi,
I need to get data without submitting a form.So i need to use ajax.I dont know how to implement ajax..
I tried to do something from net.I copied ajax.php in libraries folder and i copied all scripts also.I need fire ajax call at onclick how to do this one.Please help me on this...

#2
[eluser]Colin Williams[/eluser]
CodeIgniter runs on the server. It is responsible for delivering the server response. Understand that first.

I like to work with JSON or HTML when doing Ajax. So, simply put, this is what I respond with from CodeIgniter.

#3
[eluser]rajyakshmik[/eluser]
Can you give me any example codes please.I need response without refreshing page that response i need to display within div..

#4
[eluser]umefarooq[/eluser]
hi i have tried with jquery code hope it will help you alot, here on click of a link having id click im posting data, post first parameter is url, second parameter is data and third you can get data from php code and put in specific div, in url im calling welcome controller's function test, either you can give the path of your ajax file also. you can put you ajax file in system/plugins folder

javascript code
Code:
$('#click').click(function(){
    
    $.post('index.php/welcome/test',
    { name: "John", time: "2pm" },
    function(data){
        $('#content').append(data);
    });
});

php code
Code:
function test(){
        $data = $this->input->get_post('name');
        echo 'im here...'.$data;
    }

#5
[eluser]rajyakshmik[/eluser]
Can u please give me clear explanation how to call ajax in view and all

#6
[eluser]Bramme[/eluser]
Okay, here's some code, I hope it helps. What I have: a table with links with id's stored in the id attribute of the row. When I click on one of the links, I want to fetch some html that corresponds to that ID.

This is the JS: it checks all links for the click event. If clicked, it catches the parent table row and it's id. Next, it makes an ajax call to the server and sends the id with post data. When the ajax call is succsful, I add the html after the parent table row, if something goes wrong, I just alert about it.

Finally, the return false is there so the link the user clicks on isn't actually followed by the browser.
Code:
$(document).ready(function() {

    var links = $('.preview');

    links.click(function() {
        
        var par = $(this).parents('tr')
        var id = par.attr('id');
        
        $.ajax({
                type: 'POST',
                url: '/svn/handinhand/admin/gallery/get_html',
                data: 'id='+id,
                success: function(html) {
                    var html = '<tr id="gallery_preview"><td colspan="5">' + html + '</td></tr>';
                    par.after(html);
                },
                error: function() {
                    alert('Something went horribly wrong');    
                }
        });
        
        return false;
    });

});

This is my php code, plain and simple:

Code:
function get_html() {
        echo $this->mdl_gallery->get_gallery_html($this->input->post('id'));
    }
The function just calls a method that creates a bunch of html, based on the id. I simply echo that html, no need for views or so (but you could use them offcourse).

#7
[eluser]rajyakshmik[/eluser]
Please tell me the steps how to call ajax.I need to call ajax when i click button..

#8
[eluser]Bramme[/eluser]
Okay, the only way we can explain this better, is if we write a complete tutorial. Which allready exist. Please go through the trouble of googling smth before asking.

#9
[eluser]rajyakshmik[/eluser]
ok thanks

#10
[eluser]obiron2[/eluser]
Following on from Bramme's reply

Reading your posts, I am not sure you really understand how Ajax, HTTP, PHP and Code Igniter work together.

You need to understand in some depth how a web browser works with HTTP. HTTP is a whole bunch of data that your web browser will render based on some rules. The browser does not care how this HTTP data is generated; it could be PHP, ASP or a static page. PHP is used to generate HTTP data strings. CodeIgniter is a framework that makes the job of using PHP easier.

Ajax is a method of requesting HTTP data from the server without navigating away from the current page. Ajax sends a request to the server and waits for a response. It does not care how this response is generated. When it gets the response it does something with it.

Work on understanding Ajax and write some really simple pages on your local machine so that you really get what Ajax does. You will probably want to use one of the Ajax packages (mootools, jQuery etc) but it helps to know what it is doing in the back ground.

The simple answer to your original question is 'In exactly the same way as you would for a full page refresh' Your http request goes to www.sitename.com/controller/function/param1/param2 and CI will return an http data string which you then use to update part of the DOM.

jQuery and mootools do for Ajax what CI does for PHP. They provide you with shortcuts and standard functions that you can rely on to work efficiently and cut down on code wasteage.

When you know how to send an ajax request and display the results, come back to us and we will be glad to help you render the results or do some of the more complex things like build the URL string from the DOM.

Obiron


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.