0

如何通过使用 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());
} 

?>

这是章节.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 来删除一行我认为删除查询确实出错了..

4

3 回答 3

0

好的,几个修复:

  1. 您正在发送 JSON 而不是 GET
  2. 我假设您只想在删除后执行动画,所以将您的动画放在success. (如果没有,请忽略这个)。
  3. 不要注释掉data
  4. 替换idchapter_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;
    });
    
于 2013-06-12T09:55:17.943 回答
0

您快到了,您需要将要删除的 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());
} 

?>
于 2013-06-12T09:43:32.507 回答
0

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'];

更多信息

于 2013-06-12T09:45:35.120 回答