简体   繁体   English

如何使用Ajax和Jquery删除MySQL记录?

[英]How do I delete a MySQL record using Ajax and Jquery?

I'm not sure how to get the post title into the jquery .ajax call. 我不知道如何将帖子标题放入jquery .ajax调用中。 I am able to create and display my blog posts, now I'm trying to add functionality to delete and edit. 我能够创建和显示我的博客帖子,现在我正在尝试添加删除和编辑功能。 I'm starting with delete since it's obviously the easier of the two. 我从删除开始,因为它显然更容易。 How do I get the blog title from the post array into my functions.js file so that I can delete the matching record in my Database? 如何将post数组中的博客标题导入到我的functions.js文件中,以便删除数据库中的匹配记录? Also... Thanks! 还有...谢谢!

HTML HTML

 <?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><button id="editPost" type="button">Edit</button><button id="deletePost" type="button">Delete</button><button id="commentPost" type="button">Comment</button></div>';
        }

?>

Functions.php 的functions.php

$function= $_POST['function']; 
$title = $_POST['title'];
if($function == "deletePost")
 deletePost($title)
 function deletePost($title){
$sql = "DELETE FROM blog WHERE Title = '$title';";
mysqli_query($mysqli, $sql);
}

Functions.js Functions.js

$(document).ready(function(){
    $('#deletePost').on('click', function(){
        $.ajax({
            url: 'functions.php',
            data:{function: "deletePost", title: "how do I get the blog title here"}
            success: function(data){
         //confirmation of deletion
        }
        });
    });
});

Since you are expecting a POST request, you'll need to specify that while making the AJAX request. 由于您需要POST请求,因此您需要在发出AJAX请求时指定它。

$.ajax({
    url: 'functions.php',
    type: 'POST', // specify request method as POST
    ...
});

That should do it. 应该这样做。

Try This 尝试这个

<?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><button id="editPost" type="button">Edit</button><a class="deletePost" rel="'. $row['Title'] .'" href="#" >Delete</a><button id="commentPost" type="button">Comment</button></div>';
            }

?>

$(document).ready(function(){
    $(".deletePost").on('click', function(){
        $.ajax({
            url: 'functions.php',
            type: 'POST',
            data:{function: "deletePost", title: $(this).attr('rel')}
            success: function(data){
         //confirmation of deletion
        }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM