Welcome Guest, Not a member yet? Register   Sign In
CI using Prototype

[eluser]Rey Philip Regis[/eluser]
Hey guys, I need an advice with reagrds to writing ajax using Code Igniter. I made some simple sample code using CI and Prototype and its running. What I want to know now, is if Im doing it the right way, I mean if what Ive done (the coding style) is correct. Please give me some advice on this.


Step 1: Create an htaccess file and configure the config.php.

Step 2: Put the put the scriptaculous folder (with scriptaculous.js and prototype.js) in the system folder.

Strp 3: Code

Heres the code:

Controller: (ajax.php)

class Ajax extends Controller
    public function Ajax()
    public function showMessage()
        echo 'Good Morning everybody.';
    public function index()
        $data['title'] = 'Simple Ajax using Code Igniter & Prototype';
        $this->load->view('simple_ajax.php', $data);

View: (simple_ajax.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;

function callAjax()
    var url = 'http://localhost/ci/ajax/showMessage';
    new Ajax.Request(
                            method: 'GET',
                            onFailure: function()
                              $('display')[removed] = 'Failed'
                            onSuccess: function(transport)
                              $('display')[removed] = transport.responseText                            }



    <div id="display">No Message</div>

        $data = array(
                        'name' => "button",
                        'id' => "button",
                        'type' => "button",
                        'content' => "Click",
                        'onClick' => "callAjax()"
        echo form_button($data);

Any advice to improve?

By the way that's not [removed] that's innerHTML. Why does that change?

Good day.....

About the only thing I may suggest is to populate your AJAX URL with the CI base_url() function. Once you write about 30 AJAX calls and then want to push live, it will save you the trouble of searching for all those URL strings and replacing them.

Otherwise, it looks like you're right on track.

[eluser]Nick Husher[/eluser]
I'd suggest to move your scripts to an external file if you plan on doing anything too sophisticated. Build the site to work with no scripting and layer the scripts on top.

[eluser]Rey Philip Regis[/eluser]
Thanks for the advice guys....

this thread is not working... =(

the easiest way to use ajax with codeigniter is simply treat ajax in normal way.
dont try to integrate it with codeigniter.

Theme © iAndrew 2016 - Forum software by © MyBB