Il est possible d'utiliser les frameworks javascript connus :
ou de mettre en oeuvre un framework Javascript s'intégrant à CodeIgniter : CJAX
Copier le contenu du zip dans les destinations suivantes :
Dans cjax/config.php,
$config->fallback = true;
Options -Indexes RewriteEngine on RewriteBase /yourSiteURL/ RewriteRule ^/?ajax/(.*)$ ajax.php?$1 [QSA,L] RewriteCond $1 !^(index\.php|assets|font|bootstrap|cjax|robots\.txt|ajaxfw\.php|ajax\.php) RewriteRule ^(.*)$ index.php/$1 [L]
Avec CJAX, Les contrôleurs se situent dans le dossier response de application, et sont accessibles par l'url ajax.php?nomController.
Afficher un message sur la page sur le click d'un élément :
<?php
class TestAjax extends CI_Controller{
public function click(){
$this->load->view('vClick');
}
}
?>
<?php
require_once "ajax.php";
$ajax = ajax();
$ajax->Exec("btClick",$ajax->update("divResponse","Test de click"));
?>
<html>
<head>
<meta charset="UTF-8">
<title>Message sur click</title>
<?php echo $ajax->init();?>
</head>
<body>
<h2>message sur click du button</h2>
<input type='button' id='btClick' value='Cliquer sur le bouton'>
<div id="divResponse">divResponse</div>
</body>
</html>
Il s'agit de faire une requête vers une page (/test/affiche/) sur le click d'un bouton, et d'afficher le résultat dans une div
<?php
class TestAjax extends CI_Controller{
public function click(){
$this->load->view('vClick');
}
public function get(){
$this->load->view('vGet');
}
}
?>
<?php
require_once "ajax.php";
$ajax = ajax();
$ajax->Exec('btGet' , $ajax->divResponse=$ajax->call("test/affiche"));
?>
<html>
<head>
<meta charset="UTF-8">
<title>GET</title>
<?php echo $ajax->init();?>
</head>
<body>
<h2>Get</h2>
<input type='button' id='btGet' value='Cliquer sur le bouton'>
<div id="divResponse">divResponse</div>
</body>
</html>
Poster des variables vers une URL sur le click d'un élément :
<?php
class TestAjax extends CI_Controller{
public function click(){
$this->load->view('vClick');
}
public function get(){
$this->load->view('vGet');
}
public function post(){
$this->load->view('vPost');
}
public function postResult(){
var_dump($_POST);
}
}
?>
<?php
require_once "ajax.php";
$ajax = ajax();
$vars = array(
'hello' => 'world',
'world' => 'hello!',
'someVar' => 'someValue',
'x' => 'y'
);
$ajax->post = $vars;
$ajax->Exec('btPost' , $ajax->call("ajax.php?testAjax/postResult",'divResponse'));
?>
<html>
<head>
<meta charset="UTF-8">
<title>POST</title>
<?php echo $ajax->init();?>
</head>
<body>
<h2>POST</h2>
<input type='button' id='btPost' value='Cliquer sur le bouton'>
<div id="divResponse">divResponse</div>
</body>
</html>
Soumettre un formulaire existant vers une URL :
public function getForm(){
$this->load->view('vForm');
}
public function formSubmit(){
$ajax = ajax();
$ajax->alert("Champs soumis : ".print_r($_POST,1));
}
<?php
require_once "ajax.php";
$ajax = ajax();
$ajax->click("btSubmit",$ajax->form("ajax.php?testAjax/formSubmit","frm"));
?>
<html>
<head>
<meta charset="UTF-8">
<title>SubmitForm</title>
<?php echo $ajax->init();?>
</head>
<body>
<h2>SubmitForm</h2>
<form id="frm" name="frm" method="post" action="">
<div><label for="nom">Nom : </label><input type="text" id="nom" name="nom"></div>
<div><label for="prenom">Prénom : </label><input type="text" id="prenom" name="prenom"></div>
<input type='submit' id='btSubmit' value='Valider'>
</form>
<div id="divResponse"></div>
</body>
</html>
public function formSubmit(){
$ajax = ajax();
$ajax->divResponse="Champs soumis : ".print_r($_POST,1);
}