我是ajax新手,我试图在不刷新选项卡的情况下查看、添加、编辑和删除mysql数据库的数据,或者换句话说,在此表上使用ajax:
我已经完成了 View 部分,*已编辑,但我不知道如何编辑或删除*。我知道这是一项很长的任务,但我在互联网上没有找到解决方案..提前致谢
HTML 代码:
<html>
<head>
<title>View Data Without refresh</title>
<script language="javascript" type="text/javascript" src="script/jquery-git.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
(function() {
$.ajax({
type: "POST",
url: "display.php",
dataType: "html",
success: function(response){
$("#responsecontainer").html(response);
}
});
});
});
</script>
</head>
<body>
<fieldset><br>
<legend>Manage Student Details</legend>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Class</th>
<th>Section</th>
<th>Status</th>
</tr>
</table>
<div id="responsecontainer" align="center"></div>
</fieldset>
<input type="button" id="display" value="Add New"/>
</body>
</html>
PHP显示代码:
<?php
include("connection.php");
$sql = "select * from tbl_demo";
$result=mysqli_query($db,$sql);
echo "<table class='myTable'>";
while($data = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td width='13.5%'>$data[0]</td>";
echo "<td width='21%'>$data[1]</td>";
echo "<td width='19.5%'>$data[2]</td>";
echo "<td width='24%'>$data[3]</td>";
echo "<td><span class='edit'>Edit</span> | <span
class='delete'>Delete</span></td>";
echo "</tr>";
}
echo "</table>";
?>
最佳答案
如果您的连接
、sql查询
和php响应
正常并且
I want it to be automatically done
表示您希望在页面加载时运行 ajax。那么,php 的输出应该是在最后,而不是在每次迭代中。
<?php
include("connection.php");
$sql = "select * from tbl_demo";
$result=mysqli_query($db,$sql);
$output = "<table class='myTable'>";
while($data = mysqli_fetch_row($result))
{
$output .="<tr>";
$output .="<td width='13.5%'>$data[0]</td>";
$output .="<td width='21%'>$data[1]</td>";
$output .="<td width='19.5%'>$data[2]</td>";
$output .="<td width='24%'>$data[3]</td>";
$output .="<td><span class='edit'>Edit</span> | <span
class='delete'>Delete</span></td>";
$output .="</tr>";
}
$output .="</table>";
echo $output;
?>
要进行编辑/删除,您需要将带有操作的页面传递或重定向到名为 Controller 的其他页面。再次进行更改后,您需要重定向 index/view data
页面,或者如果您不想刷新/重定向页面,请使用 ajax。
关于javascript - 如何在不刷新的情况下编辑和删除MySQL数据库的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819143/