在表中显示ajax响应


Display ajax response in Table

display.html:

<div id="display_result" style="display: none"><table class="table">
<p style="float: right;" >Select All<input type="checkbox" class="allcb" data-child="chk" checked/> </p>                                    
<thead>
  <tr>
     <th>Die No</th>    
     <th> Status </th>    
     <th> Location </th>    
     <th>Select</th>
  </tr>
</thead>
<tbody>
</table>
<div id ="issue_button">       
<input type="submit" id="submit" class="btn btn-success " value="Recieve" style="width: 150px;"></div>  
</div>

Ajax:

var data = JSON.stringify($("#form").serializeArray());
// alert(data);
$.ajax({ // Send the credential values to another checker.php using Ajax in POST menthod
type: 'POST',
data: {
list: data
},
url: 'die_recieving_process.php',
success: function(data) ){
$('#display_result').html(data);
}
});

die_recieving_process.php

while($fetch = mysql_fetch_array($query))
{
if($fetch[1] == "Table Rack" )
{
echo '<tr class="success"><td>'.$fetch[0].'</td><td>'.$fetch[1].'</td><td>'.$fetch[3] . '</td> <td><input type=checkbox class="chk" id=check_box value= '.$fetch[2].' name= check_list[]  </td>  </tr>';
}
else
{
echo '<tr class="warning"><td>'.$fetch[0].'</td><td>'.$fetch[1].'</td><td>'.$fetch[3] . '</td> <td><input type=checkbox class="chk"  id=check_box value= '.$fetch[2].' name= check_list[] checked </td>  </tr>';
}    
}   

你好,朋友们,在display.html中,我必须显示在die_recieving_process.php中处理的结果。在ajax中,我已经将所有值发送到die_recieng_process.php,在获取结果后,我必须在displayhtml 中显示结果

首先在Javascript中,您有两个错误:您的代码覆盖div的现有内容,div是整个表。。。在成功函数声明中有一个不必要的括号

所以改变这个:

success: function(data) ){
$('#display_result').html(data);
}

对此:

success: function(data) {//remove unnecessary bracket
   $('#display_result tbody').html(data);//add data - to tbody, and not to the div
}

顺便说一下,使用$.post(),您可以将javascript代码写得更短,如下所示:

var data = JSON.stringify($("#form").serializeArray());
$.post('die_recieving_process.php',{list:data},function(responseData){
    $('#display_result tbody').html(responseData); //added to tbody which is inside #display_result
    $('#display_result').show();
});

第二步您需要关闭表内的tbody标记

创建带有空body标记和body id=tBody的html表,例如:

<table>
  <caption>Smaple Data Table</caption>
  <thead>
    <tr>
      <th>Field 1</th>
      <th>Field 2</th>
    </tr>
  </thead>
  <tbody id="tBody"></tbody>
</table>

点击加载按钮assuming that my json file is storing userData后,使用jquery ajax在创建的表中加载json数据,如userName, age, city:

$('#btnLoadAll').click(function () {
                $.ajax({
                    url: "url/data.json",
                    dataType: 'json',
                    success: function (resp) {
                        var trHTML = '';
                        $.each(resp, function (i, userData) {
                                trHTML +=
                                    '<tr><td>'
                                    + userData.userName
                                    + '</td><td>'
                                    + userData.age
                                    + '</td><td>'
                                    + userData.city 
                                    + '</td></tr>';
                            
                        });
                        $('#tBody').append(trHTML);
                    },
                    error: function (err) {
                        let error = `Ajax error: ${err.status} - ${err.statusText}`;
                         console.log(error);
                    }
                })
            });

如果您没有看到结果,请尝试删除display.html 中的style="display: none"