如何通过使用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来删除行 我认为删除查询错了..
答案 0 :(得分:0)
你几乎就在那里,你需要将要删除的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());
}
?>
答案 1 :(得分:0)
您在jquery中发送JSON
类型,但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'];
答案 2 :(得分:0)
好的,一些修复:
success
内。 (如果没有,请忽略这一点。)data
将id
替换为chapter_id
变量
info
$(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;
});