Data Search using Jquery Ajax: it is the very helpful tutorial for jQuery learner and who use jQuery in codeigniter framework for access page without page reload.
This tutorial will be helpful for search user details without page refresh using jQuery in your codeigniter web application. Give a user id or user name and display user details single clik on a button.
You need to follow some process for it:
1) Create a search input box with a button, where you click data are display in a table.
2) Define a table in the same page for display user details.
3) Create a controller and pass the search value into the controller function name.
4) Access the model page and pass the value and select the all user details using where clause.
That’s like:
Follow the example and source code:
HTML Page:
<div class="row"> <h1 class="mainTitle"> Search Result</h1> </div> <div class="row"> <div class="form-group"> <input type="text" id="user_id" class="form-control" placeholder="Enter User ID"> </div> <button type="button" onclick="result_search()"> Search </button> </div> <h3>Search Result Found</h3> <div class="row"> <table class="table table-striped table-hover" id="sample-table-2"> <thead> <tr> <th>Full Name</th> <th>User Id</th> <th>Address</th> </tr> </thead> <tbody id="studentdata"> </tbody> </table> </div>
Script:
<script type="text/javascript"> function result_search(){ var id=document.getElementById('user_id').value; $.ajax({ type: "post", url: "<?php echo base_url('Controller/get_result');?>", data: {u_id:id}, cache: false, success: function(value){ jQuery('#studentdata').html(value); console.log(html); } }); } </script>
Controller:
<?php public function get_result(){ $id=$this->input->post('u_id'); $data=$this->Model->getResult($id); //print_r($data); die; $view = ''; foreach ($data as $stu) { $view = $view ."<tr>"; $view = $view ."<td>".$stu->name."</td>"; $view = $view ."<td>".$stu->u_id."</td>"; $view = $view ."<td>".$stu->address."</td>"; $view = $view ."</tr>"; } echo $view; } ?>
Model:
<?php public function getResult($id){ $qry=$this->db->select('*')->from('table name')->where('u_id',$id)->get()->result(); return $qry; } ?>