CodeIgniter Forums
Ajax dynamic data fetch - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Ajax dynamic data fetch (/showthread.php?tid=66789)



Ajax dynamic data fetch - sheenam - 12-03-2016

Hi all,

i want to fetch the data dynamically from two tables
my model , view and controller are as follows
VIEW


PHP Code:
[code][php  <div class="form-group">
 
           <label for="contact_type" class="col-sm-1 hidden-xs control-label hidden col-xs-2"><?php echo get_phrase('contact_type');?>: </label>
            <div class="col-sm-5 col-xs-5">
          <select name="role" id="role" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('roles');?> *</option>
                    <?php
                  
                    foreach 
($roles as $role) { 
 
                   ?>
                        <option value="<?=$role->role_id?>"><?=$role->role_name?></option>
                    <?php ?>
                </select>
            </div>
            <div class="col-sm-5 col-xs-5">
                <select name="assigned_to" id="assigned_to" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('assigned_to');?> *</option>

                    
                </select>
            </div>
        </div> 

// THIS IS MY SCRIPT//
<script>
   //AJAX call for getting items for selected supplier
   $('select#role').change(function() {
       var contact_type = $(this).val();
      // console.log(role);
       var link = '<?php echo base_url()?>'+'index.php/deals/get_contacts_ajax/'+contact_type;
       $.ajax({
           supplier_id: contact_type,
           url: link
       }).done(function(option) {
//            console.log(option);
           if (option) {
               $('#assigned_to').html(option);
           }else{
               $('#assigned_to').html('<option><?php echo get_phrase('no_record_found'); ?></option>');
           };
           rowOption = option;
       });
   });

</script>


[/php]





CONTROLLER
PHP Code:
 public function get_contacts_ajax($type)
 
   {
 
       if (!is_numeric($type)) return FALSE;
 
       $items $this->deal_model->get_contacts_ajax($type);
 
       $str '<option value="">'.get_phrase('select').' *</option>';
 
       if ($type == 1) {
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->role_id.' '.$value->role_name.'</option>';
 
           }
 
       }else{
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->name.'</option>';
 
           }
 
       }
 
       echo $str;
 
   }
 
    
MODEL
PHP Code:
public function get_contacts_ajax($type)
 
   {
 
       
         
        return $this
->db->get('users')->result();
 
   

i have to fetch the details from roles and users in which i have to fetch role names from roles table and user-name from users with reference to role_id . i tried to do that but i am unable to get the results. 
i am able to get the different roles from database but how do i get the users related to that id using AJAX
please help me !!

Thanku in Advance.


RE: Ajax dynamic data fetch - neuron - 12-03-2016

(12-03-2016, 01:26 AM)sheenam Wrote: Hi all,

i want to fetch the data dynamically from two tables
my model , view and controller are as follows
VIEW


PHP Code:
[code][php  <div class="form-group">
 
           <label for="contact_type" class="col-sm-1 hidden-xs control-label hidden col-xs-2"><?php echo get_phrase('contact_type');?>: </label>
            <div class="col-sm-5 col-xs-5">
          <select name="role" id="role" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('roles');?> *</option>
                    <?php
                  
                    foreach 
($roles as $role) { 
 
                   ?>
                        <option value="<?=$role->role_id?>"><?=$role->role_name?></option>
                    <?php ?>
                </select>
            </div>
            <div class="col-sm-5 col-xs-5">
                <select name="assigned_to" id="assigned_to" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('assigned_to');?> *</option>

                    
                </select>
            </div>
        </div> 

// THIS IS MY SCRIPT//
<script>
   //AJAX call for getting items for selected supplier
   $('select#role').change(function() {
       var contact_type = $(this).val();
      // console.log(role);
       var link = '<?php echo base_url()?>'+'index.php/deals/get_contacts_ajax/'+contact_type;
       $.ajax({
           supplier_id: contact_type,
           url: link
       }).done(function(option) {
//            console.log(option);
           if (option) {
               $('#assigned_to').html(option);
           }else{
               $('#assigned_to').html('<option><?php echo get_phrase('no_record_found'); ?></option>');
           };
           rowOption = option;
       });
   });

</script>


[/php]





CONTROLLER
PHP Code:
 public function get_contacts_ajax($type)
 
   {
 
       if (!is_numeric($type)) return FALSE;
 
       $items $this->deal_model->get_contacts_ajax($type);
 
       $str '<option value="">'.get_phrase('select').' *</option>';
 
       if ($type == 1) {
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->role_id.' '.$value->role_name.'</option>';
 
           }
 
       }else{
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->name.'</option>';
 
           }
 
       }
 
       echo $str;
 
   }
 
    
MODEL
PHP Code:
public function get_contacts_ajax($type)
 
   {
 
       
         
        return $this
->db->get('users')->result();
 
   

i have to fetch the details from roles and users in which i have to fetch role names from roles table and user-name from users with reference to role_id . i tried to do that but i am unable to get the results. 
i am able to get the different roles from database but how do i get the users related to that id using AJAX
please help me !!

Thanku in Advance.

I think your error is here: $str = '<option value="">'.get_phrase('select').' *</option>';
instead of get_phrase('select') write $this->get_phrase('select')

My advice to you is always run controller methods without ajax, test it if it work properly and then try using ajax.


RE: Ajax dynamic data fetch - sheenam - 12-04-2016

(12-03-2016, 02:13 AM)neuron Wrote:
(12-03-2016, 01:26 AM)sheenam Wrote: Hi all,

i want to fetch the data dynamically from two tables
my model , view and controller are as follows
VIEW


PHP Code:
[code][php  <div class="form-group">
 
           <label for="contact_type" class="col-sm-1 hidden-xs control-label hidden col-xs-2"><?php echo get_phrase('contact_type');?>: </label>
            <div class="col-sm-5 col-xs-5">
          <select name="role" id="role" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('roles');?> *</option>
                    <?php
                  
                    foreach 
($roles as $role) { 
 
                   ?>
                        <option value="<?=$role->role_id?>"><?=$role->role_name?></option>
                    <?php ?>
                </select>
            </div>
            <div class="col-sm-5 col-xs-5">
                <select name="assigned_to" id="assigned_to" class="form-control bottom-border" required>
                    <option value=""><?php echo get_phrase('assigned_to');?> *</option>

                    
                </select>
            </div>
        </div> 

// THIS IS MY SCRIPT//
<script>
   //AJAX call for getting items for selected supplier
   $('select#role').change(function() {
       var contact_type = $(this).val();
      // console.log(role);
       var link = '<?php echo base_url()?>'+'index.php/deals/get_contacts_ajax/'+contact_type;
       $.ajax({
           supplier_id: contact_type,
           url: link
       }).done(function(option) {
//            console.log(option);
           if (option) {
               $('#assigned_to').html(option);
           }else{
               $('#assigned_to').html('<option><?php echo get_phrase('no_record_found'); ?></option>');
           };
           rowOption = option;
       });
   });

</script>


[/php]





CONTROLLER
PHP Code:
 public function get_contacts_ajax($type)
 
   {
 
       if (!is_numeric($type)) return FALSE;
 
       $items $this->deal_model->get_contacts_ajax($type);
 
       $str '<option value="">'.get_phrase('select').' *</option>';
 
       if ($type == 1) {
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->role_id.' '.$value->role_name.'</option>';
 
           }
 
       }else{
 
           foreach ($items as $value) {
 
               $str.='<option value="'.$value->role_id.'">'.$value->name.'</option>';
 
           }
 
       }
 
       echo $str;
 
   }
 
    
MODEL
PHP Code:
public function get_contacts_ajax($type)
 
   {
 
       
         
        return $this
->db->get('users')->result();
 
   

i have to fetch the details from roles and users in which i have to fetch role names from roles table and user-name from users with reference to role_id . i tried to do that but i am unable to get the results. 
i am able to get the different roles from database but how do i get the users related to that id using AJAX
please help me !!

Thanku in Advance.

I think your error is here: $str = '<option value="">'.get_phrase('select').' *</option>';
instead of get_phrase('select') write $this->get_phrase('select')

My advice to you is always run controller methods without ajax, test it if it work properly and then try using ajax.

Thanks for your reply dear. I jus want to know how can i use ajax for the two table dynamically. like i m fetching data from 2 tables and based on one drop down value the values related to that field shud come how it can be done? plz help!!


RE: Ajax dynamic data fetch - InsiteFX - 12-05-2016

This may help you out.

More Cascading AJAX Dropdowns with CodeIgniter


RE: Ajax dynamic data fetch - sheenam - 12-05-2016

(12-05-2016, 04:05 AM)InsiteFX Wrote: This may help you out.

More Cascading AJAX Dropdowns with CodeIgniter

thanku so much dear :Smile understood a little bit may be the link u have pasted out here would make my concepts much clear. thanku so much .. Smile