Welcome Guest, Not a member yet? Register   Sign In
Ajax dynamic data fetch
#1

(This post was last modified: 12-03-2016, 01:27 AM by sheenam.)

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.
Reply
#2

(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.
Reply
#3

(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!!
Reply
#4

This may help you out.

More Cascading AJAX Dropdowns with CodeIgniter
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB