我希望請求從sql刪除記錄而不更改網頁。不幸的是,當我點擊這個按鈕時它實際上並沒有刪除記錄。 post_action.php導致刪除失敗的問題是什麼?正確使用.ajax jquery方法?
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>';
}
?>
我不會在前端發送整個SQL查詢。我只需發送相關表格或WHERE值。否則,您很可能會打開數據庫進行濫用。 –
爲什麼你使用職位標題來刪除使用ID而不是標題 –
因爲它是一個虛擬的例子來幫助我學習傢伙。這是在我的本地主機不是一個網站與交通:) – CodeManiak