无法通过ajax实时调用进行删除


Wont delete via ajax live call

当我按下删除按钮时,它不会删除内容。我认为ajax删除调用有问题

ajax-file.php

    $('.delete_update').live("click",function() {
        var ID = $(this).attr("id");
        var dataString = 'msg_id='+ ID;
        if(confirm("Sure you want to delete this update? There is NO undo!")) {
        $.ajax({
            type: "POST",
            url: "delete_data.php",
            data: dataString,
            cache: false,
            success: function(html){
            $("div#bar"+ID).slideUp('slow', function() {$(this).remove();});
        }
    });
    }
return false;
});
<div class="accordionButton" id="bar<?php echo $msg_id; ?>"><?php echo $msg_id; ?>:<?php echo $name; ?>
<a href="#" id="<?php echo $msg_id; ?>" class="delete_update">X</a>
</div>
<div class="accordionContent" style="display: block;"><?php echo $msg; ?></div>

delete_data.php

<?php
include("db.php");
if($_POST['msg_id'])
{
$id=$_POST['msg_id'];
$id = mysql_escape_String($id);
$sql = "delete from messages where msg_id='$id'";
mysql_query( $sql);
}
?>

如果使用最新的jQuery,则使用on而不是live,因为它不支持

*已更新动态内容

$(document).on("click", '.delete_update', function() {

我认为问题出在这个函数中。您给$.ajax数据对象一个字符串。函数"正常"仅处理javascript对象。

在不检查参数的情况下处理参数时也要小心。看看为mysql准备的语句

    $.ajax({
        type: "POST",
        url: "delete_data.php",
        data: dataString, <--- May be wrong because you are using POST
        cache: false,
        success: function(html){
        $("div#bar"+ID).slideUp('slow', function() {$(this).remove();});
    }

试试这个。尝试在服务端脚本处回显msg_id

    $.ajax({
        type: "POST",
        url: "delete_data.php",
        data: {msg_id:ID},
        cache: false,
        success: function(html){
        $("div#bar"+ID).slideUp('slow', function() {$(this).remove();});
    }