通过javascript/jquery运行php sql查询以隐藏条目
在我的网站上,我试图在不刷新屏幕的情况下删除(隐藏)表中的sql条目 当前,每个可见条目都显示在id为schedule000的div中,其中000是条目的id号。每个都有一个删除按钮:通过javascript/jquery运行php sql查询以隐藏条目,php,javascript,jquery,mysql,ajax,Php,Javascript,Jquery,Mysql,Ajax,在我的网站上,我试图在不刷新屏幕的情况下删除(隐藏)表中的sql条目 当前,每个可见条目都显示在id为schedule000的div中,其中000是条目的id号。每个都有一个删除按钮: <div id="btn_delete" class="schedule-delete" onclick="deleteEvent(schedule'.$row['id'].');"></div> 我在网上搜索后发现了get功能,但我似乎无法让它工作。 我在寻找建议或新的解决方案 谢谢
<div id="btn_delete" class="schedule-delete" onclick="deleteEvent(schedule'.$row['id'].');"></div>
我在网上搜索后发现了get功能,但我似乎无法让它工作。
我在寻找建议或新的解决方案
谢谢
作为旁注:
这是它正在调用的页面的一部分
if (isset($_GET['d'])) {
$id = $_GET['d'];
$query = "UPDATE schedule SET visible=0 WHERE id='$id'";
if (!(mysql_query($query) or die(mysql_error()))) {
echo 'failed to delete';
}
编辑:使用Sven的方法,我现在有:
function del_item() {
try {
var item_id = $(this).attr('item_id');
var item = $(this).parent().parent(); //parent().paren... until you reach the element that you want to delete
$.ajax({
type: 'POST',
url: "../schedule/index.php",
data: { id: item_id},
success: function() {
//fade away and remove it from the dom
$(element).fadeOut(300, function() { $(this).remove(); });
},
error: function() {
alert('failed to delete');
}
});
} catch (err) {
alert(err.message);
}
}
以及文档准备功能和
if (isset($_POST['action'])) {
if ($_POST['action'] == 'd' && isset($_POST['id'])) {
$id = $_POST['id'];
$query = "UPDATE schedule SET visible=0 WHERE id='$id'";
if (!(mysql_query($query) or die(mysql_error()))) {
echo 'failed to delete';
}
die("J! :)");
}
您正在以字符串形式发送要删除的id。查询变成“WHERE id='001',但应该是“WHERE id=1”。因此,在将id添加到delurl变量之前,请使用“parseInt”将javascript中的id解析为整数
var id = parseInt(id.id.substring(8));
var delurl = "../schedule/index.php?d="+id;
...etc
从查询中删除“”我会这样做:
<div id="btn_delete" class="schedule-delete" item_id="' . $row['id'] . '"></div>
将项目id添加到删除按钮,如下所示:
<div id="btn_delete" class="schedule-delete" item_id="' . $row['id'] . '"></div>
对于php删除页面(url\u to\u your\u remove\u script.php
):
您可以在此处找到有关$.ajax的更多信息:
希望这有帮助
PS:没有测试代码,但它应该可以工作。使用
GET
方法实际删除数据是个坏主意,这就是POST
的目的。请停止使用古老的mysql_*函数编写新代码。它们不再被维护,社区已经开始运行。相反,你应该学习准备好的语句并使用它们或者。如果您想学习,。(另外:将数据发送到。/schedule/
,而不是。/schedule/
)非常感谢您指出我的“/”错误。我理解您为什么说不使用get,但我如何使用POST而不刷新页面?(在GET中,我将添加一个检查,以确定谁正在尝试使用该功能。此外,我将查看您的PDO建议您允许用户通过url更新您的数据库,确保他们是具有正确凭据的有效用户。也许他已将其id设置为数据库中的字符串。这不是最明智的做法,但无法更改。)我知道…在编辑了你的代码之后,我让它开始工作了。谢谢!我希望我能帮你…但是我没有15个代表。
function del_item() {
var item_id = $(this).attr('item_id');
var item = $(this).parent(); //parent().paren... until you reach the element that you want to delete
$.ajax({
type: 'POST',
url: "url_to_your_remove_script.php",
data: { id: item_id},
success: function() {
//fade away and remove it from the dom
$(item).fadeOut(300, function() { $(this).remove(); });
},
error: your_error_func
});
}
$(document).ready(function() {
$('.schedule-delete').click(del_item);
});
<?php
if(isset($_POST['id'])) {
//your update query here
die("J! :)");
}
//no id, return error
header('HTTP/1.1 500 Internal Server Error :(');
?>