[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">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
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 }
}
);
}
</head>
<body>
<div id="display">No Message</div>
<?php
$data = array(
'name' => "button",
'id' => "button",
'type' => "button",
'content' => "Click",
'onClick' => "callAjax()"
);
echo form_button($data);
?>
</body>
</html>
Any advice to improve?
By the way that's not [removed] that's innerHTML. Why does that change?
Good day.....