[英]jquery ajax call for deleting a row in mysql
如何通过仅使用jquery ajax调用传递ID来删除行,这就是delete.php
<?php
require_once('DB.class.php');
$dbconnect = new DB('schoollife', 'root', '');
$id=$_GET['chapter_id'];
$del="DELETE FROM sl_chapter WHERE chapter_id=".$id ;
$result=mysql_query($del);
// if successfully deleted
if($result){
echo "<font color='red'>record deleted successful</font>";
//echo "<BR>";
//echo "<a href='delete.php'>Back to main page</a>";
}
else {
echo "<br/>";
die('Error: ' . mysql_error());
}
?>
这是Chapter.html
<script>
$(document).on('click','.delbutton',function(){
var del_id = element.attr("id");
var info = 'id=' + del_id;
if(confirm("Are you sure you want to delete!"))
{
$.ajax({
type: "json",
url: "delete.php",
//data: info,
success: function(){
}
});
$(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
</script>
我如何通过仅使用jquery ajax调用传递ID来删除行,我认为删除查询确实出错了。
您已经快到了,您需要将要删除的ID传递给delete.php
Chapter.html:
<script>
$(document).on('click','.delbutton',function(){
var del_id = element.attr("id");
if(confirm("Are you sure you want to delete!"))
{
$.ajax({
type: "POST", //changed
url: "delete.php",
data: 'id=' + del_id, // changed
success: function(){
}
});
$(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
</script>
delete.php:
require_once('DB.class.php');
$dbconnect = new DB('schoollife', 'root', '');
$id=$_POST['id']; //changed
$del="DELETE FROM sl_chapter WHERE chapter_id=".$id ;
$result=mysql_query($del);
// if successfully deleted
if($result){
echo "<font color='red'>record deleted successful</font>";
//echo "<BR>";
//echo "<a href='delete.php'>Back to main page</a>";
}
else {
echo "<br/>";
die('Error: ' . mysql_error());
}
?>
您发送JSON
jQuery的类型,但type
应该是POST
或GET
$.ajax({
type: "GET", // should be GET or POST not JSON
url: "delete.php",
data: info,
success: function(){
}
});
还要在delete.php
更改它
$id=$_GET['chapter_id'];
至
$id=$_GET['id'];
好,几个修复:
success
。 (如果没有,请忽略此)。 data
将id
替换为info
变量中的chapter_id
$(document).on('click','.delbutton',function(){ var del_id = element.attr("id"); var info = 'chapter_id=' + del_id; if(confirm("Are you sure you want to delete!")) { $.ajax({ type: "get", url: "delete.php", data: info, success: function(){ $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow"); } }); } return false; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.