我做了一个简单的评论系统here,我想创建一个删除按钮,如果按下该注释将被删除(基本上从数据库中删除)。我不知道如何在评论中实现这一点。我已经使javascript与delta.php进行交互,但我不知道如何制作delete.php。 mySQL字段是;
如何编写delete.php以与mySQL和评论系统进行交互?谢谢!
<script type="text/javascript">
function deleteUser(id){
new Ajax.Request('delete.php', {
parameters: $('idUser'+id).serialize(true),
});
}
</script>
答案 0 :(得分:0)
您可以使用jquery简化操作:
$('#delete_button').click(function(){
var comment_id = $(this).attr('id');//you should use the id of the comment as id for the delete button for this to work.
$.post('delete.php', {'comment_id' : comment_id}, function(){
$(this).parent('div').remove(); //hide the comment from the user
});
});
delete.php将包含以下内容:
<?php
//include database configuration file here.
//If you don't know how to do this just look it up on Google.
$comment_id = $_POST['comment_id'];
mysql_query("DELETE comment from TABLE WHERE comment_id='$comment_id'");
//mysql_query is not actually recommended because its already being deprecated.
//I'm just using it for example sake. Use pdo or mysqli.
?>
答案 1 :(得分:0)
以下是一个例子:
<强>的Javascript 强>:
function deleteComment(id)
{
$.ajax(
{
type: "POST",
url: "delete.php?id="+id,
dataType: "html",
success: function(result)
{
if(result == "Ok") alert("The comment is successfuly deleted");
else alert("The following error is occurred: "+result);
}
});
}
PHP(delete.php):
if(!isset($_POST['id'])) die("No ID specified");
$id = mysql_real_escape_string($_POST['id']);
mysql_query("DELETE FROM `comments` WHERE `id` = $id") or die(mysql_error());
echo "Ok";