Dave
Dave

Reputation: 114

AJAX request(delete a comment)

I'm trying to delete a mysql record with javascript but I fail.

So this is my js function

function delpost(id){
    if(confirm('Are you sure?')){
        $('#comment_'+id).hide();
        http.open("get","/index.php?p=delcomment&id=" + id, true);
        http.send();
    }
}

And this is my delcomment.php(included through index.php)

$comment_id = $_GET['id'];

if(logged() && $status=="administrator"){
    $delquery = mysql_query("DELETE FROM comments WHERE id='$comment_id' LIMIT 1");
    die();
}else{
    die();
}

I hope you can help me with this :)

Upvotes: 1

Views: 1104

Answers (1)

clyc
clyc

Reputation: 2450

update: try using

http.send(null)

instead of

http.send()

also, use firebug to see if your ajax request is actually being sent to the server

better solution: (php rusty!)

delcomment.php

$comment_id = $_POST['id'];
$comment_id = mysql_real_escape_string($comment_id);

if(logged() && $status=="administrator"){
    $query = "DELETE FROM comments WHERE id='{$comment_id}'";
    $result = mysql_query($query, $con);
     die();
}else{
    die();
}

using jquery to post (make sure to include the jquery.js), your javascript function should be like this:

function delpost(id){
    if(confirm('Are you sure?')){

        $.ajax({
            type: "POST",
            url: "/index.php",
            data: {p: "delcomment", id: id},
            success: function(){
                $('#comment_'+id).hide();
            },
            error: function(){
                alert('failure');
            }
        });     
    }
}

Upvotes: 2

Related Questions