• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic fetch of data

#1
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:

VIEW

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


<html>

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

 <section class="content-header">
    
      <ol class="breadcrumb">
        <li><a href="#">Unassign&nbsp;<i class="fa fa-times"></i></a></li>
        
      </ol>
    </section>
    <br>
<?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">
 Products
</div>
 </div>
 </div>    
 
 <?php
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">
                            <tr>
                                 <!--  <th><input class='check_all' type='checkbox' onclick="select_all()"/></th>-->
                                <thead>
    <th>ID</th>
   <th>Brand Name</th>
    <th>Short Name</th>
    <th>Long Description</th>
    <th>Cost Price</th>
    <th>Selling Price</th>
    <th>Supplier</th>
    <th>Inventory</th>
    <th colspan="2">Action</th>
    </thead>
            </tr>
            <tr>
            <!--<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>
            </tr>
            </table>
                         
            <button type="button" class='btn btn-success addmore'>+ Add More</button>
                </div>
                </form>
            </div>    
            
        </div>    
    </div><!-- /container -->
           
    
                       <?php
        
}
        
?>
         
              

                </form>
        
            
        </div>
    </div>
</div>


</span>
</div>
</div>
    
</div>
</div>
</section>
</body>

</html> 
MODEL
PHP Code:
MODEL:
[
php]function searchproduct($id)
{

$this->db->select('p_name','p_short_name','p_long_description','p_costprice','p_sellingprice');
    $this->db->from('product_master');

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

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

array_push($data$name); 
}
echo 
json_encode($data);
    $query $this->db->get();    
  
return $query->result();

}



PLZ help me on this i am stuck thanku in advance.. !! 
[/php]

CONTROLLER:
PHP Code:
<?php

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

class 
Products extends CI_Controller {

 
   public function __construct() {
 
       parent::__construct();
 
       $this->load->library('form_validation');
 
        if (!$this->session->userdata('is_admin_login')) {
 
           redirect('admin/home');
 
       }
 
   }

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

public function 
search($id)
{

    
$id=$this->input->get('id'); 

if(
$_POST['type'] == 'product_table')
        {
            
$data = array(
$row_num='row_num' => $this->input->post('row_num'),
$name='name' => $this->input->post('name_startsWith')
);
    
array_push($data$name);    
            }
    echo 
json_encode($data);    
 
$this->load->model('admin/insert_products');  
   $data
['records']=$this->insert_products->searchproduct($id);  
         
//return the data in view  
 
        $this->load->view('admin/vwProducts'$data); 
         


i am stuck please help me with this..thanku so much in advance.
Reply

#2
It would be helpful if you would edit your question and include the javascript that makes the ajax call.
Reply

#3
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:
<script>
$(
document).ready(function() {
    
    $(
'#categories').change(function(){
        var 
category = $('#categories').val();
        $.
ajax({
            
url"<?php echo base_url();?>products/search",
            
type"POST",
            
data: { cat category } ,
            
success: function(rdata){
     
                  $('#products').html(rdata);
            }
        });
    });
});
</
script

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');
 
 $this->db->like('product_category',$category);
 
 $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:
$this->load->view('view_name');
Reply

#4
thanku so much for your replies. let me try. if stuck plz help me get through. thanu so much once again.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.