【问题标题】:How to edit and delete records of MySQL database without refreshing?如何在不刷新的情况下编辑和删除MySQL数据库的记录?
【发布时间】:2017-12-02 19:13:57
【问题描述】:

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

【问题讨论】:

  • 我没有看到按钮。您的 ajax 代码设置为在文档加载后运行。你的问题标题很模糊。你到底在这里问什么?您遇到的具体问题是什么?
  • 我也听不懂问题..
  • 有'编辑 |删除'Status'表标题中使用的两个跨度,如果用户单击编辑,则应编辑数据,如果用户单击删除,则应删除数据

标签: javascript php jquery mysql ajax


【解决方案1】:

如果您的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;
?>

要进行编辑/删除,您需要将带有操作的页面传递或重定向到名为控制器的其他页面。再次进行更改后,如果您不想刷新/重定向页面,则需要重定向 index/view data 页面或使用 ajax。

【讨论】:

  • @HaseebHassy :如果您希望通过 ajax 进行所有 CRUD 操作意味着没有页面刷新,那么是的,所有东西都将在 javascript 中。
猜你喜欢
  • 2020-02-26
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 2013-03-19
相关资源
最近更新 更多