如何通过使用 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
章节.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>
删除.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());
}
?>
关于php - jquery ajax调用删除mysql中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17061856/