CodeIgniter Forums

Full Version: dynamic fetch of data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all,
i want to fetch data dynamically from database using ajax call with one select and other autofill using ajax call
i am unable to my code is like this:


PHP Code:
<?php include_once "vwHeader.php" ?>


table {
table , thead
    background:    #4682B4;
table, th, td {
    border: 1px solid white;
    border-collapse: collapse;
th, td {
    padding: 5px;
    text-align: left;
table#t01 tr:nth-child(even) {
    background-color: #eee;
table#t01 tr:nth-child(odd) {
table#t01 th {
    background-color: black;
    color: white;
<div class="content-wrapper">

 <section class="content-header">
      <ol class="breadcrumb">
        <li><a href="#">Unassign&nbsp;<i class="fa fa-times"></i></a></li>
<?php  include_once "vwLeadshead.php" ?> 
 <section class="content">
 <div class="row">

<?php include_once "vwMenu.php" ?> 

 <div class="col-lg-9 col-xs-9">  
<div class="box box-primary">
<div class="box-header">
foreach ($records as $row 
    <div class="container-fluid">
        <div class="row main-content">           
           <div class="col-xs-9 col-xs-9">
            <div class="input-group">
             <?php echo form_open('admin/products/search?id='.$row->l_id,['class'=>'form-horizontal panel','method'=>'post']); ?>

    <div class="table-responsive">
     <table id="example1" class="table table-bordered table-striped">
                                 <!--  <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>-->
   <th>Brand Name</th>
    <th>Short Name</th>
    <th>Long Description</th>
    <th>Cost Price</th>
    <th>Selling Price</th>
    <th colspan="2">Action</th>
            <!--<td><input type='checkbox' class='case'/></td>-->
            <td><span id='snum'>1.</span></td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_name_1' name='p_name[]'/></td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_short_name_1' name='p_short_name[]'/></td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_long_description_1' name='p_long_description[]'/></td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_costprice_1' name='p_costprice[]'/> </td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_sellingprice_1' name='p_sellingprice[]'/> </td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_supplier_1' name='p_supplier[]'/> </td>
            <td><input class="form-control autocomplete_txt" type='text' id='p_inventory_1' name='p_inventory[]'/> </td>
            <td><a href="">Edit</a> </td>
            <td><a href="">Delete</a> </td>
            <button type="button" class='btn btn-success addmore'>+ Add More</button>
    </div><!-- /container -->



PHP Code:
php]function searchproduct($id)


    $this->db->where('u_id' ,$id); 

->db->where("column LIKE '".strtoupper($name)."%'"); 
// Also mention table name here

    $query $this->db->get();    
return $query->result();


PLZ help me on this i am stuck thanku in advance.. !! 

PHP Code:

if (!defined('BASEPATH'))
   exit('No direct script access allowed');

Products extends CI_Controller {

   public function __construct() {
        if (!$this->session->userdata('is_admin_login')) {

function index() {
$query $this->db->get("product_master"); 
   $data['records'] = $query->result(); 

public function 


$_POST['type'] == 'product_table')
$data = array(
$row_num='row_num' => $this->input->post('row_num'),
$name='name' => $this->input->post('name_startsWith')
//return the data in view  

i am stuck please help me with this..thanku so much in advance.
It would be helpful if you would edit your question and include the javascript that makes the ajax call.
Well, start with putting code in your view for the 2 select lists that you want to use. I can't find any.
Use <select></select> as surrounding html tags. The selectable options are created with <option></option>.
Make sure they both have a name and an ID.
The select lists must be inside <form></form> tags.

Make sure jQuery is loaded in the head section of your view.
At the bottom of the view, put the javascript code that makes the Ajax call. The trigger for that is a change in your first select list. Let's say this has the ID "categories", and you want to fill the list with ID "products" dynamically. The Ajax call could look like this:

PHP Code:
document).ready(function() {
category = $('#categories').val();
url"<?php echo base_url();?>products/search",
data: { cat category } ,
success: function(rdata){

In your controller, you collect the records from your database that have the selected category to build the html-code for the second (dependent) select list. At the end of the function, you echo that as a string:

PHP Code:
public function search()
 $html '';
 $category $this->input->post('cat');
 $query $this->db->get('products');
 //you could also use the model to retrieve the records
 if ($query->num_rows() > ) {
    foreach($query->result_array() as $row) {
       $html .= '<option value="' $row['product_name'] . '">' $row['product_name'] . '</option>';
 echo $html;

On more tip: never include a view inside another view with the php command "include". In CI you can nest views simply with:
thanku so much for your replies. let me try. if stuck plz help me get through. thanu so much once again.