views:

80

answers:

3

Hello all, I have a function on my that has a delete button to erase comments. I have the button all linked up and ready to go. I just don't know what I should put on the 'delte.php' script I am creating, and what to tell it php wise to send to the db to make the status dead. as well as deleting the comment off of the page.

Thanks

+1  A: 

You mean something like

mysql_query("DELETE * FROM yourTable WHERE commentId = $id", $yourDbLink);

Bobby

Bobby
yeah, but how do I do it to where it 1.does not have to go to a different page and deletes it right on the spot (without reloading the page)? I'm assuming a link is not needed
Ralph The Mouf
For better security, use: `$id = intval($_POST['id'])`, or else you could regret it.
The Wicked Flea
@Ralph The Mouf - you'll need to submit a post request via AJAX to `delete.php`, then capture the response and remove the DOM element corresponding to the deleted item via some JavaScript. This is probably too broad a topic for a single stackoverflow question.
Dominic Rodger
Apparently it wasn't too much, looks like this answer together with tvor's answer will get you there.
Dominic Rodger
Alternately, redirect back to the display page after deleting the comment.
bdonlan
+2  A: 

Your php script would contain a query to delete the comment

mysql_query("delete from comments where id = $id");

Send a request using Ajax which won't reload the current webpage. Here is a snippet using jQuery

function deleteComment(commentId)
{
$.ajax({
   type: "POST",
   url: "delete.php",
   data: "id="+ commentId,
   success: function(msg){
     alert( "Comment " + commentId + " deleted" );
   }
 });
}

<a href="#" onclick="deleteComment(1)">Delete Comment 1</a>
Trevor
If you are going to use jquery, attach the deleteComment() to the links click event using jquery, instead of onclick="". Also, consider validating $id or using PDO with placeholders. Otherwise, one could delete all of your comments (imagine a commentId of " 1 OR 1=1;--")
simplemotives
A: 

If you do not want the browser to go anywhere you could send HTTP status 204, "No content".

Typically though you'd do a redirect to the same page with a notice saying "Comment deleted." or similar.

Christoffer Hammarström