Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to get data from related tables on a dropdown change event using Ajax.
My Controller and Model results are ok, but I don't know that how to display the data returned from controller in JavaScript.

Because the Data have two arrays, fees and feetype so I dont know how to display data in table, and also to display feetype matched with feetypeid in fees array.

Class Fee Model:
Sessionid, Classid, Feetypeid, FeeAmount

FeeType Model:
Feetypeid, Feetype


This worked for me:

in my model I used the below code.

$data = $model->join('tblfeetype', 'tblfeetype.id = tblclassfee.feetypeid','left')->where($array)->findAll();


What I have tried:

Controller:
PHP
public function getclassfee()
    {
        $model = new ModelAjax();
        $sessionid = $this->request->getVar('sessionid');
        $classid = $this->request->getVar('classid');
        $data =  $model->getclassfee($sessionid,$classid);
        echo json_encode($data);
    }


Model:

PHP
public function getclassfee($sessionid,$classid)
   {
       $model = new ModelClassFee();
       $tmodel = new ModelFeeType();
       $array = ['sessionid' => $sessionid, 'classid' => $classid];
       $data['fees'] = $model->where($array)->findAll();
       $data['feetypes'] = $tmodel->findAll();
        return $data;
   }


JavaScript:

JavaScript
$("#classid").change(function(){
        var classid=$(this).val();
        var sessionid = $('#sessionid').val();
        $.ajax({
            url : "<?php echo site_url('Ajax/getclassfee'); ?>",
            method : "POST",
            data : {sessionid : sessionid, classid : classid},
            async : true,
                dataType : 'json',
                success: function(data)
                {
                    var html = '';
                        var i;
                        for(i=0; i<data.length; i++){
                            html += '<tr><td>'+data[i][1].feetype+'</td><td>'+data[i].feeamount+'</td>';
                            html += '<td><a href="<?= site_url('ClassFee/editclassfee/');?>'+data[i].id+'"class="btn btn-primary btn-sm"> Edit </a> | ';
                            html += '<a href="<?= site_url('ClassFee/delclassfee/'); ?>'+data[i].id+'" class="btn btn-danger btn-sm"> Delete</a></td>';
                            html += '</tr>'; 
                        }
                        $('#classfeedetail').html(html);
                }
            });
            return false;
       });
Posted
Updated 13-Jun-22 8:07am
v2

1 solution

Answered only to remove from the unanswered queue - resolved by OP.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900