【发布时间】:2020-09-28 07:10:21
【问题描述】:
附加了错误输出的屏幕截图。操作链接不起作用。]2我正在使用 ajax 从 php 中的 mysql 数据库中获取数据并使用返回的数据填充表。 HTML 表格正在填充数据,但操作链接(编辑、查看和删除)不起作用。
index.php
<div id="employee_table">
<table class="table table-bordered text-center">
<thead class="bg-dark text-center text-white">
<tr>
<th>Employee Name</th>
<th>Address</th>
<th>Gender</th>
<th>Designation</th>
<th>Age</th>
<th>Photo</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody id="tableBody">
<?php
$query = "SELECT * FROM tbl_employee ORDER BY name";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["address"]; ?></td>
<td><?php echo $row["gender"]; ?></td>
<td><?php echo $row["designation"]; ?></td>
<td><?php echo $row["age"]; ?></td>
<td><img src='<?php echo $row["image"]; ?>' width=80 height=30/></td>
<td><a id="<?php echo $row["id"]; ?>" class="btn btn-warning btn-sm edit_data"/><i class="fa fa-pencil-square" aria-hidden="true"></i></a></td>
<td><a id="<?php echo $row["id"]; ?>" class="btn btn-info btn-sm view_data"/><i class="fa fa-eye" aria-hidden="true"></i></a></td>
<td><a id="<?php echo $row["id"]; ?>" class="btn btn-danger btn-sm del_data"/><i class='fa fa-trash' aria-hidden='true'></i></a></td>
</tr>
<?php
}
?>
<tbody>
</table>
</div>
此时,编辑、查看和删除链接正在工作。但是在插入、更新或删除数据时,返回的链接不起作用。
scripts.js
function showUsers(){
//alert("hello");
$.ajax({
url:"fetchusers.php",
method:"GET",
dataType:"json",
success:function(data){
$('#tableBody').html(data);
}
});
}
fetchusers.php
<?php
require 'db.php';
$output="";
$query = "SELECT * FROM tbl_employee order by name";
$result = mysqli_query($connect, $query);
//$row = mysqli_fetch_array($result);
while($row=mysqli_fetch_array($result)){
$output .=
"<tr><td>".
$row['name'].
"</td> <td>".
$row['address'].
"</td><td>".
$row['gender'].
"</td><td>".
$row['designation'].
"</td><td>".
$row['age'].
"</td><td><img src=".
$row['image'].
" width=80 height=30/></td><td><a id=".
$row['id'].
"class='btn btn-warning btn-sm edit_data'><i class='fa fa-pencil-square' aria-hidden='true'></i></a></td><td><a id=".
$row['id'].
"class='btn btn-info btn-sm view_data'><i class='fa fa-eye' aria-hidden='true'></i></a></td><td><a id=".
$row['id'].
"class='btn btn-danger btn-sm del_data'><i class='fa fa-trash' aria-hidden='true'></i></a></td></tr>";
}
if($output){
echo json_encode($output);
}
else{
echo "No data found.";
}
?>
此时,编辑、查看和删除链接不起作用。没有事件正在发生。数据正在填充到 html 表中。
【问题讨论】:
-
成功函数中fetchusers.php返回的输出是什么?你为什么要做 json_encode($output)?我认为应该返回纯字符串,因为代码中没有 JSON 初始化。
-
即使在删除 json 编码后,表格仍在填充,但操作链接不起作用。
-
你没有提到 fetchuser 返回的输出。
-
编辑了问题。附上输出截图。操作链接无效。
-
编辑了问题。附上输出截图。操作链接无效。