我希望请求从sql中删除一条记录而不更改网页。不幸的是,当我单击此按钮时,它实际上并没有删除记录。 post_action.php有什么问题导致删除失败?
post_action.php
<?php
if($_GET['action'] == "deletePost")
deletePost($_GET['title'])
function deletePost($title){
$sql = "DELETE FROM blog WHERE Title = '$title';";
mysqli_query($mysqli, $sql);
}
?>
Ajax 请求:
$('#deletePost').click(function(){
$.ajax({
url:"post_action.php",
data: {action: "deletePost", postTitle: $(this).siblings("h3.blog").text()},
success: function(result){
$('ul.left').html(result);
}
});
});
index.php
<?php
include 'scripts/db_connect.php';
include 'scripts/functions.php';
sec_session_start();
$sql = "SELECT * FROM blog";
$result = mysqli_query($mysqli, $sql);
while($row = mysqli_fetch_array($result))
{
echo'<div class="blog"><h3 class="blog">' . $row['Title'] .
"</h3><h3>" . $row['Date'] . "</h3><h3>" . $row['Tag'] .
"</h3><hr>";
echo'<p class="blog">' . $row['Body'] . '</p><form name="postForm"
method="post" action="process_post.php">
<input type="radio" name="postAction"
value="editPost" class="postButton" type="button">Edit</input>
<input type="radio" name="postAction" value="deletePost"
class="postButton" type="button">Delete</input>
<input type="radio" name="postAction" value="commentPost"
class="postButton" type="button">Comment</input>
</form></div>';
}
?>
最佳答案
问题; 您正在发送数据:
`data: {action: "deletePost", postTitle: $(this).siblings("h3.blog").text()}`,
但你正在使用
$_GET['title'] instead use $_GET['postTitle']
其次:如果请求成功,则调用成功函数。因此,您可以在其中执行许多操作,例如:
1. show a confirmation box sayin deleted successfully.
2. show a div giving confirmation.
3. remove the deleted row(should be done in all options)
关于javascript - .ajax jquery 方法的正确使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20258890/