How To Role Ajax Alongside Jquery In Addition To Codeigniter Tutorial?

You tin easily usage ajax amongst jQuery as well as Codeigniter. Below is the tutorial amongst consummate rootage code on how to usage ajax amongst jQuery as well as Codeigniter.

Controller : ajax_sample.php
<?php degree Ajax_sample extends CI_Controller {       role __construct(){         parent::__construct();         $this->load->helper('url');     }       /*      * demonstrate listing every bit a table, larn information from "test_model"      * */     role get_list_view(){           $this->load->model('test_model');           $data = array();           $data['title'] = 'Lorem ipsum';         $data['list'] = $this->test_model->get_data();           $this->load->view('sample_table', $data);       }       role index(){         $this->load->view('test_page');     }   } 
Model : test_model.php
<?php   //file: application/models/test_model.php    degree Test_model extends CI_Model {       role __construct(){         parent::__construct();     }       role get_data(){           $type = $this->input->post('type');           if($type != 1){             furnish array();         }           furnish array(             array(                 'name' => 'Abigail',                 'email' => 'ut.sem.Nulla@duinecurna.org',                 'registered_date' => '01/17/2014'             ),             array(                 'name' => 'Ralph',                 'email' => 'ultrices.posuere@Sed.org',                 'registered_date' => '10/08/2013'             ),         );     }   } 
Views: test_page.php
<html> <head> <title>Ajax sample (with CodeIgniter)</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <script type="text/javascript">  var controller = 'ajax_sample';  var base_url = '<?php echo site_url(); //you convey to charge the "url_helper" to usage this role ?>';  role load_data_ajax(type){  $.ajax({   'url' : base_url + '/' + controller + '/get_list_view',  'type' : 'POST', //the agency y'all desire to ship information to your URL  'data' : {'type' : type},  'success' : function(data){ //probably this asking volition furnish anything, it'll survive pose inwards var "data"  var container = $('#container'); //jquery selector (get chemical gene yesteryear id)  if(data){ container.html(data);  }  }  });  }  </script>   </head>  <body>  <button onclick="load_data_ajax(1)">Load listing (type 1)</button>  <button onclick="load_data_ajax(2)">Load listing (type 2)</button>   <hr />   <div id="container"> </div> </body> </html> 
Views: sample_table.php

<?php if(isset($title) && !empty($title)) : ?>     <h1><?php echo $title; ?></h1> <?php endif; ?>  <?php if(isset($list) && count($list)) : ?>     <table>         <thead>             <tr>              <th>Name</th>              <th>E-mail</th>              <th>Registered date</th>             </tr>         </thead>         <tbody>             <?php foreach($list every bit $row) : ?>                 <tr>                     <td><?php echo $row['name']; ?></td>                     <td><?php echo $row['email']; ?></td>                     <td><?php echo $row['registered_date']; ?></td>                 </tr>             <?php endforeach; ?>         </tbody>     </table> <?php else : ?>     <h3>No results found!</h3> <?php endif; ?>


Source: https://github.com/alyssonajackson/sample_ajax_with_jquery_and_codeigniter
Sumber http://developer-paradize.blogspot.com

Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?