在表中显示AJAX成功响应

时间:2017-06-15 10:56:15

标签: php jquery codeigniter

如何显示表中的值..我无法显示该值。在ajax中,我已经发送了所有值,并在从数据库中检索记录之后。我要在表格中显示结果

输出

 [{"ID":"15","patient_name":"Sangeeta","patient_email":"sangeetha@gmail.com","gender":"Female","age":"26","address":"Jayanagar","city":"Bangalore","laboratory_name":"Anand","laboratory_address":"bsk","laboratory_place":"bengaluru","referral_pat_id":"18","active":"1","created_on":"2017-06-13"}]

如何检索每个值

AJAX脚本

$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
    var rowid = $(e.relatedTarget).data('id');
    alert(rowid);
    console.log(rowid);
    $.ajax({
        type : 'post',
        url : 'url', //Here you will fetch records 
        data :  'id='+ rowid, //Pass $id
        success : function(resp){
          alert(resp);
         var trHTML = '';
                    $.each(resp, function (i, userData) {
                        for (i = 0; i < resp.UserData.length; i++) {
        alert(trHTML);

                            trHTML +=
                                '<tr><td>'
                                + resp.userData[i].ID
                                + '</td><td>'
                                + resp.userData[i].patient_name
                                + '</td><td>'
                                + resp.userData[i].patient_email 
                                + '</td></tr>';
                        }
                    });
                    $('#tBody').append(trHTML);
        }
    });
  });
 });

控制器

 public function fetch_records()
 {
    print_r($_POST);
    $this->load->model('Physician_confirm_m');

    $id = $_POST['id']; //escape string
    print_r($id);
    $result=$this->Physician_confirm_m->fetch_history_records($id);
    echo json_encode($result);      
}

模型

 public function fetch_history_records($id)
 {
 $this->db->where('referral_pat_id',$id);
 $this->db->from('referral_confirmation_details');
 $q = $this->db->get();
 return $q->result();
 }

HTML

 <button type="button" class="btn btn-info disablebtn" style="text-align: center;" data-toggle="modal" data-target="#myModal" data-id="<?php echo $post->referral_patient_id;?>"><i class="fa fa-history" aria-hidden="true"></i><strong> HISTORY</strong></button>

<div class="modal-body"> 
      <table>

        <tbody id="tBody"></tbody>
      </table>
 </div>
</div>

1 个答案:

答案 0 :(得分:0)

你可以用另一种方式来实现这一目标。

$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
    var rowid = $(e.relatedTarget).data('id');

    console.log(rowid);
    $.ajax({
        type : 'post',
        url : 'url', //Here you will fetch records 
        data :  'id='+ rowid, //Pass $id
        success : function(resp){
           var obj = jQuery.parseJSON(resp);
           $('#tBody').append(obj.ajaxPage);
        }
    });
  });
 });

在您的控制器中应该像

public function fetch_records()
 {

    $this->load->model('Physician_confirm_m');

    $id = $_POST['id']; //escape string

    $data['result']=$this->Physician_confirm_m->fetch_history_records($id);

    $result['ajaxPage'] = $this->load->view('pages/result_table', $data, true);
    echo json_encode($result);      
}

result_table视图页面应为

<?php if($result){ foreach($result as $row){ ?>
<tr>
 <td><?=$row['ID']?></td>
  <td><?=$row['patient_name'];?></td>
  <td><?=$row['patient_email'];?></td>;
</tr>
<?php 
   } 
}?>