[英]Display ajax response inside table
I want to display data from ajax response inside table. 我想在表格中显示来自ajax响应的数据。
Here is my table: 这是我的表:
<table data-toggle="table" data-show-columns="true" data-search="true" data-select-item-name="toolbar1" id="menu_table">
<thead>
<tr>
<th class="centerText" data-field="item_id">ID</th>
<th class="centerText" data-field="name">Name</th>
<th class="centerText" data-field="price">Price</th>
<th class="centerText" data-field="image">Image</th>
<th class="centerText" data-field="edit">Edit</th>
</tr>
</thead>
<tbody style="text-align:center;" id="menu_table_data"></tbody>
</table>
This is menu_table.php which gets data from database and returns response to ajax : 这是menu_table.php ,它从数据库获取数据并返回对ajax的响应:
$output = '';
$search = mysqli_query($link,"SELECT menu.id, menu.name, menu.price, menu.image ORDER BY menu.id ASC;");
while($data = mysqli_fetch_array($search))
{
$output .= '<td>'.$data['id'].'</td>
<td>'.$data['name'].'</td>
<td>'.$data['price'].'</td>
<td><div id="div_image">
<img src="uploaded_images/'.$data['image'].'" class="thumbnail" height="100" width="80" /></div></td>
<td><a class="btn btn-primary glyphicon glyphicon-edit" role="button"></a></td>
';
}
echo $output;
This is ajax function: 这是ajax功能:
$(document).ready(function(){
function fetch_data(){
$.ajax({
url: "menu_table.php",
method: "POST",
success: function(data){
$('#menu_table_data').html(data);
}
});
}
fetch_data();
});
$('#menu_table_data').html(data); $( '#menu_table_data')HTML(数据)。 is showing data but not like table data. 显示数据但不像表数据。 How can i show it inside table properly? 如何在桌子内正确显示?
You have missed <tr></tr>
. 你错过了<tr></tr>
。
try this: 尝试这个:
$output = '';
$search = mysqli_query($link,"SELECT menu.id, menu.name, menu.price, menu.image ORDER BY menu.id ASC;");
while($data = mysqli_fetch_array($search)) {
$output .= '
<tr>
<td>'.$data['id'].'</td>
<td>'.$data['name'].'</td>
<td>'.$data['price'].'</td>
<td>
<div id="div_image">
<img src="uploaded_images/'.$data['image'].'" class="thumbnail" height="100" width="80" /></div>
</td>
<td>
<a class="btn btn-primary glyphicon glyphicon-edit" role="button"></a>
</td>
</tr>
';
}
echo $output;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.