简体   繁体   中英

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. 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? Also... Thanks!

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

$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

$(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.

$.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
        }
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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