数据如何使用AJAX从数据库中删除行

时间:2018-01-03 12:33:32

标签: javascript php ajax database connection

这是我的PHP代码

  

AjaxServer.php

include '../include/connection.php';

//here check the prediction 

if(isset($_POST["delete_me"]) && $_POST["delete_me"]=="true"){

    $id = $_POST["id"];

    $table = $_POST["table"];

    $query = "delete from {$table} where id='{".$id."}'";

    if(mysql_query($query)){

      echo "record has been deleted";

    }

}

这是我的js文件Custom.js

 $(document).ready(function() {

    $('a.delete').click(function(e) {

        if(confirm("Do you realy want to delete this")){

            e.preventDefault();

            var parent = $(this).parents("tr");

            var table = $(this).attr("data-table");

            var id = $(this).attr("id");

            var data ="id="+id+"&delete_me=true&table="+table;

            $.ajax({

                type: 'post',

                url:'include/ajaxserver.php',

                data: data,

                beforeSend: function() {

                    parent.animate({'backgroundColor':'#fb6c6c'},300);
                },
                success: function() {

                    parent.slideUp(500,function() {parent.remove();});
                }
            });

        }
        else{

         return false;

         }

    });/*end of the cick function*/

});/*end of the ready function*/

1 个答案:

答案 0 :(得分:0)

您没有将数据发送到ajax文件,请通过以下方式正确替换ajax代码:

my $sendmail = "/usr/sbin/sendmail -t -i";

my $MSG=<<EOM;
...
EOM

print $MSG; # print message to STDOUT for inspection

open( SENDMAIL, "|$sendmail" ) or die "Cannot open $sendmail: $!";
print SENDMAIL $MSG;
close(SENDMAIL);

希望这段代码有用。