how to deal with delete buttons in php?
I want to know what is the best way to handle delete or edit buttons?
So let's say from a comment box, should I use post or g开发者_StackOverflowet method for this and then make some validations in the page that is suppose to delete the comment?
As a specific example, I'm gonna point out facebook's comment box, it has a little button for deleting comments, but it doesn't display the url
in the bottom of the browser, so I guess this uses post method with some jQuery in it. I'm I right? if not what should I use for this type of buttons?
I don't know what Facebook uses, but yes, you should use POST
. You might want to use something like this:
<div class="comment">
<p><code>POST</code> should be used for actions which change data.</p>
<form action="comments/1/delete" method="post" class="delete-form">
<input type="submit" value="Delete" />
</form>
</div>
$(".delete-form").submit(function() {
var me=$(this);
$.ajax({
type: 'POST',
url: me.attr('action'),
success: function() {
me.parent().remove();
},
error: function() {
alert("Oops! An error occurred while deleting your comment.");
}
});
return false;
});
If you don't like that the delete button is on a separate line, just style it a bit with CSS:
.comment > p,
.comment > .delete-form {
display: inline;
}
You are right. To solve that kind of issues you can either do a classical form which will reload a page after the click or rely on AJAX. AJAX is an asynchronous way to communicate with a server. In your case, when someone clicks on the button you want it to transmit a request to the server to perform the deletion or edition without reloading the page. Check out the JQuery get and post functions for a quick start on the topic:
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.post/
In the very case of a delete button, you will do a post since it is going to change something on the server side.
精彩评论