使用 PHP、JS/jQuery 和一点 AJAX 的 CRUD 方法
我一直在开发一个系统,您可以在其中创建、读取、更新、删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTML:
Javascript:
如果您使用的浏览器不支持 DELETE,请将类型:“DELETE”更改为类型:“POST”。
HTML:
Javascript:
Change type: 'DELETE' to type: 'POST' if you're working with browsers that don't support DELETE.
您可以让 Ajax 调用返回新的值列表,然后用新内容替换整个表。
在回调中:
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 callback:
div
that contains thetable
results of the call (by making a call to PHP if that's what's generating the table)
to the
div
That's something we do often, although, we need to since it's a multi-user system.