如何在不刷新的情况下编辑和删除MySQL数据库的记录?

时间:2017-06-29 07:44:37

标签: javascript php jquery mysql ajax

我是ajax的新手,我正在尝试查看,添加,编辑和删除mysql数据库的数据而不刷新选项卡,换句话说使用此表上的ajax:

我已完成视图部分,*已编辑,但我无法弄清楚如何编辑或删除*。我知道这是一项非常漫长的任务,但我在互联网上找不到任何解决方案..提前致谢

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>";
?>

1 个答案:

答案 0 :(得分:2)

如果您的connectionsql queryphp response没问题,

  

我希望它能自动完成

表示您希望在页面加载时运行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。

相关问题