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

[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.

Steps:

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:

Code:
Controller: (ajax.php)

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

Code:
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;head&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(
                        url,
                        {
                            method: 'GET',
                            onFailure: function()
                            {
                              $('display')[removed] = 'Failed'
                            },
                            onSuccess: function(transport)
                            {
                              $('display')[removed] = transport.responseText                            }
                        }
                     );
}


&lt;/head&gt;

&lt;body&gt;

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

    &lt;?php
        $data = array(
                        'name' => "button",
                        'id' => "button",
                        'type' => "button",
                        'content' => "Click",
                        'onClick' => "callAjax()"
                        );
                        
        echo form_button($data);
    ?&gt;
    
&lt;/body&gt;
&lt;/html&gt;

Any advice to improve?

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

Good day.....
#2

[eluser]jcopling[/eluser]
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.
#3

[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.
#4

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

[eluser]paulon[/eluser]
this thread is not working... =(
#6

[eluser]CI_avatar[/eluser]
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