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());
} 

?>

これは 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を渡すだけで行を削除するにはどうすればよいですか?削除クエリが間違っていたと思います..

4

3 に答える 3

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>

削除.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

わかりました、いくつかの修正:

  1. GET の代わりに JSON を送信しています
  2. 削除後にのみアニメーションを実行することを想定しているため、アニメーションを 内に配置しますsuccess。(そうでない場合は、これを無視してください)。
  3. をコメントアウトしないでくださいdata
  4. 内部変数idに置換chapter_idinfo

    $(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

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 に答える