简体   繁体   English

我在ajax成功中收到我的数据如何在表中显示jquery数据

[英]I am receiving my data in ajax success how can I show jquery data in table

I fetch data from controller and it shows in console.log(data) and I want to show that data in table 我从控制器获取数据,它显示在console.log(data) ,我想在表中显示该数据

View: 视图:

<?php include 'header.php';?>
<script src="/assets/js/plugins/jquery.min.js"></script>
<script src="/assets/js/jquery.validate.min.js"></script>
<script>
 var jq = $.noConflict();
</script> 
<table id="enquiry_table">
<thead>
 <tr>
    <th>From</th>
    <th>TO</th>
    <th>Date</th>
    <th>Status</th>
 </tr>
</thead>
<tbody>
   <?php 
      foreach ($records as $rec){
    ?>
  <tr>
      <td><?php echo $rec[0]->lr_from; ?></td> 
      <td><?php echo $rec[0]->lr_to; ?></td>               
      <td><?php echo date('d-m-Y',strtotime($rec->date));?></td>
      <td><?php echo $rec->status;?> </td> 
  </tr>
    <?php
    }
  ?>
</tbody>
</table>

<script type="text/javascript">
  function status_form(){
  var lr_no = jq('#lr_no').val();
     jq.ajax({
          url :"https://demo.barque.online/sitecontroller/StatusController/fetchStatus",
          type:"POST",
          dataType: "json",
          data:{
            lr_no:lr_no,
         },
          success: function(data)
          {
            console.log(data);
         },
          error:function(data)
          {
            alert("error message"+data);
          },async:false,
      });    
    }
</script>
<?php include 'footer.php'; ?>

Controller: 控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class StatusController extends CI_Controller {
  public function __construct()
{
    header('Access-Control-Allow-Origin: my url');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 604800');
    header("Access-Control-Allow-Headers: Origin, Content-Type, Accept, Access-Control-Request-Method");
    header("Access-Control-Allow-Methods: GET, POST");
    parent::__construct();
    $this->load->model("sitemodel/StatusModel",'sModel');
 }
    function fetchStatus(){
    $lr_no                       = $this->input->post('lr_no');
    $statusResult['records']     = $this->sModel->fetchRecords($lr_no);
    echo json_encode($statusResult);
     }
}

try this:- 尝试这个:-

<script type="text/javascript">


 function status_form(){
  var lr_no = jq('#lr_no').val();
     jq.ajax({
          url :"https://demo.barque.online/sitecontroller/StatusController/fetchStatus",
          type:"POST",
          dataType: "json",
          data:{
            lr_no:lr_no,
         },
          success: function(data)
          {
            var html="";
            for(var i=0;i<data.length;i++){
               html += "<tr>";
               html += "<td>"+data[i]['your_index']+"</td>";//as per your columns and use this if your data is array. if it was stdObject then use (.)sign for indexing.
               html += "</tr>";
            }
            $('#userdata').append(html);
            console.log(data);
         },
          error:function(data)
          {
            alert("error message"+data);
          },async:false,
      });    
    }

</script>

and add this:- 并添加: -

<tbody id="userdata">

I dont know about php. 我不知道php。 But you can do that very easily with jquery with the code inside the success function. 但是你可以使用jquery很容易地使用success函数中的代码。

<script type="text/javascript">
  function status_form(){
  var lr_no = jq('#lr_no').val();
     $.ajax({
          url :"",
          type:"POST",
          dataType: "json",
          data:""
          success: function(data)
          {
            var str = "";
            str += "<table>";
            $.each(data,function(i,item){
              str += "<tr><td>" + item.YOUR_VALUE + "</td></tr>";
            });
            str += "</table>";
            $("#div").append(str);
          },
          error:function(data)
          {
            alert("error message"+data);
          },
          async:false,
      });    
    }
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试将我的 ajax 响应中的数据显示到我的 jquery 数据表中 - I am trying to show the data from my ajax response to my jquery data table 如何成功调用ajax在html表中显示字符串数据? - how to show string data in html table on success call of ajax? 如何使用 JQUERY/AJAX 显示包含图片和文本数据的多个数据,这些数据作为对象或数组发送? - How can I show multiple data containing picture and text data with JQUERY/AJAX, which are sent as object or array? 如何在“ jQuery.ajax({success:function(data)””中设置类似“ data”的回调函数参数? - How do I set a callback function parameter like 'data' in the “jQuery.ajax({ success : function(data)”? 如何在jQuery Ajax成功回调中处理我的JSON数据? - How to handle my JSON data in jQuery Ajax success callback? 为什么我不能用ajax显示我的数据? - Why can't I show my data with ajax? 我应该在jQuery的ajax成功处理程序中验证响应数据吗? - Should I validate response data in jQuery's ajax success handler? 当我的数据名称中带有“:”时,如何使用JQuery的Ajax类提交数据? - How can I submit data with JQuery's Ajax class when my data name has a “:” in it? 如何在php中通过ajax显示从数据库接收的表中的数据 - how can i show data in table received from Database through ajax in php 我如何获取jQuery Ajax成功回调中的“ clicked”元素 - How can I get in 'clicked' element in jquery ajax success callback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM