使用 PHP、JS/jQuery 和一点 AJAX 的 CRUD 方法

发布于 2024-10-31 20:04:59 字数 717 浏览 0 评论 0原文

我一直在开发一个系统,您可以在其中创建、读取、更新、删除 SQLite 数据库中的表。到目前为止,我只编写了创建和查看方法(简单)。

现在,我在决定如何删除和更新项目时遇到了障碍。在视图页面,通过php查询数据库并生成表格(通过php回显为html)。每行旁边还添加了一个按钮,看起来像一个小垃圾桶,用于删除该行。

如何使用AJAX同时从html表和sqlite表中删除行?一个更明确的问题是,如何将每个按钮链接到每一行并在单击按钮时查询数据库?

这是生成的表:

foreach($result as $entry){
 echo  '<tr>' . '<td>'. $entry['department']. ' ' . $entry['CRN']. '</td>' . 
    '<td>'. $entry['title'] . '</td>' . 
    '<td>'. $entry['addDate'] . '</td>'. 
    '<td><button class="fg-button fg-button-icon-left ui-state-default ui-corner-all"><span class="ui-icon ui-icon-trash"></span></button></td>'.'</tr>';
}

I've been working on a system where you can create-read-update-delete to/from a table in a SQLite database. So far I've only written the create and view methods (easy).

Now, I have ran into a roadblock on deciding how to go about deleting and updating items. On the view page, there is query to the database through php and a generation of the table (echoed through php into html). There is also a button added next to each row that looks like a little trash can to delete the row.

How can I use AJAX to simultaneously delete the row from the html table and sqlite table? A more definitive question would be, how can I link each button to each row and query the database when the button is clicked?

Here is the generated table:

foreach($result as $entry){
 echo  '<tr>' . '<td>'. $entry['department']. ' ' . $entry['CRN']. '</td>' . 
    '<td>'. $entry['title'] . '</td>' . 
    '<td>'. $entry['addDate'] . '</td>'. 
    '<td><button class="fg-button fg-button-icon-left ui-state-default ui-corner-all"><span class="ui-icon ui-icon-trash"></span></button></td>'.'</tr>';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2024-11-07 20:04:59
  1. 创建 PHP 控制器和操作。它必须采用 ID 作为参数,并且应该使用该 ID 对数据库执行删除命令。理想情况下,它只接受 DELETE 请求(不接受 GET 或 POST)。这并不总是可行,因为某些浏览器不支持 DELETE HTTP 请求。
  2. 在前端,创建如下所示的 HTML 和 javascript:

HTML:

<input id="clickMe" type="button" value="Delete Button" data-rowId="12"/>

Javascript:

$(document).ready(function(){
    $('#clickMe').click(function(){
        // send the delete request
        $.ajax({
            url: '/myEntity/delete',
            data: {id: $(this).data('rowId')},
            type: 'DELETE',
            // removes the row only if the delete succeeds
            success: function(){ $(this).parents('tr').remove(); }
        });
    });
});

如果您使用的浏览器不支持 DELETE,请将类型:“DELETE”更改为类型:“POST”。

  1. Create a PHP controller and action. It has to take an ID as a parameter and it should execute a delete command against your database using that ID. Ideally, it will accept only DELETE requests (not GET or POST). This isn't always possible because some browsers don't support DELETE HTTP requests.
  2. on your front end, create HTML and javascript that looks like this:

HTML:

<input id="clickMe" type="button" value="Delete Button" data-rowId="12"/>

Javascript:

$(document).ready(function(){
    $('#clickMe').click(function(){
        // send the delete request
        $.ajax({
            url: '/myEntity/delete',
            data: {id: $(this).data('rowId')},
            type: 'DELETE',
            // removes the row only if the delete succeeds
            success: function(){ $(this).parents('tr').remove(); }
        });
    });
});

Change type: 'DELETE' to type: 'POST' if you're working with browsers that don't support DELETE.

四叶草在未来唯美盛开 2024-11-07 20:04:59

您可以让 Ajax 调用返回新的值列表,然后用新内容替换整个表。

  • 在 JS 中,通过回调进行删除调用。
  • 在回调中:

    • 清空包含以下内容的 div
    • 根据以下内容创建一个新表
      调用结果(如果生成表格,则调用 PHP)
    • 附加该表
      div

这是我们经常做的事情,但我们需要这样做,因为它是一个多用户系统。

You could have the Ajax call return the new list of values and just replace the entire table with the new content.

  • In the JS, make delete call with a callback.
  • In the callback:

    • empty the div that contains the
      table
    • create a new table based on the
      results of the call (by making a call to PHP if that's what's generating the table)
    • append that table
      to the div

That's something we do often, although, we need to since it's a multi-user system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文