0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So if I use type: 'POST' then I can grab the same variables that I used on the previous php page?
When you use POST, you will be able to access all POST parameters passed to your PHP script by the AJAX request.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.