0

My code inside of function deletePost is not executing. This is because the contents of $_GET['title'] is empty. I set the value of the title in the ajax using this line postTitle: $(this).siblings("h3.blog").text() how come the value doesn't make it through the the php script?

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="editPost" type="button">Edit</input>
              <input type="radio" name="postAction" value="deletePost" class="deletePost" type="button">Delete</input>
              <input type="radio" name="postAction" value="commentPost" class="commentPost" type="button">Comment</input></form></div>';
        }

        ?>

What am I doing wrong with $_GET and the data from ajax?

JavaScript

$('.deletePost').click(function(){
    $.ajax({
        url:"scripts/post_action.php",
        data: {action: "deletePost",  postTitle: $(this).siblings("h3.blog").text()},
    });
});

post_action.php

<?php
include 'db_connect.php';
include 'functions.php';
if($_GET['action'] == "deletePost")
        deletePost($mysqli, $_GET['postTitle']);
function deletePost($mysqli, $title){
    $sql = "DELETE FROM blog WHERE Title = '$title'";
    mysqli_query($mysqli, $sql);
}
?>
CodeManiak
  • 1,755
  • 3
  • 15
  • 31
  • This means you have a syntax error in one of your PHP files. It doesn't have anything to do with JavaScript. Check `db_connection.php` and `functions.php` for syntax errors. – Felix Kling Nov 28 '13 at 08:13
  • Just did a print_r($_GET); and discovered the title is empty... I think I'm gonna delete the question and continue rubber ducking – CodeManiak Nov 28 '13 at 08:15
  • I was able to refine my question, hopefully this makes my problem more clear! – CodeManiak Nov 28 '13 at 08:24
  • Well, the title is empty because `h3.blog` is not a sibling of `input.deletePost`. I.e. `$(this).siblings("h3.blog")` doesn't select any element and calling `.text()` on an empty selection returns an empty string. That doesn't explain the syntax error though. Or are you not having the syntax error anymore? – Felix Kling Nov 28 '13 at 08:24
  • The syntax error was coming from an echo I was using for testing. I used print_r($_GET); though instead of an echo and realized that the post title wasn't getting sent through properly. With my recent updates isn't h3.blog a sibling of input.deletePost? – CodeManiak Nov 28 '13 at 08:30
  • Ah... as I said, the `h3.blog` element is not a sibling of the button. You have to traverse up the DOM first. – Felix Kling Nov 28 '13 at 08:31
  • Ohhhh is it because the form is the sibling and they are children of the form? So what would a good way to handle this issue be? (sorry I know thats a silly question but I have minimal experience and although I could find a way, I'm curious to hear your suggestion) – CodeManiak Nov 28 '13 at 08:33
  • You could probably do `$(this).closest('div.blog').find('h3.blog').text()`. – Felix Kling Nov 28 '13 at 08:34
  • I used .parent in front of the .sibling and that worked, still no delete though, but the good news is the title is finally showing up in the $_GET array. Thanks SO much for your help!! I wish you provided an answer so I could give you the checkmark – CodeManiak Nov 28 '13 at 08:34
  • Make sure your SQL query is correct. – Felix Kling Nov 28 '13 at 08:36
  • WORKING! That is the exclamation point to lots of hours of pathetic research and work for me. Thank you again! The only thing is that my index.php page that the posts are displayed on doesn't actually show that the posts are deleted until I refresh. How do I make it refresh instantly? – CodeManiak Nov 28 '13 at 08:37
  • Add a success callback to the Ajax call and remove the `div.blog` element there. – Felix Kling Nov 28 '13 at 08:42
  • Thanks again Felix, once I am more knowledgable I'll try to pay it forward ;) "If you’re not making mistakes, you’re not doing anything." – CodeManiak Nov 28 '13 at 08:43
  • :) Have a look at http://learn.jquery.com/ajax/jquery-ajax-methods/ if you want to learn more about it. – Felix Kling Nov 28 '13 at 08:45

2 Answers2

1

This is because the contents of $_GET['title'] is empty.

The h3.blog element is not a sibling of the delete button. It's a sibling of the parent of the delete button (the form element). To be flexible with your layout, you can use .closest (traverse up) with .find (traverse down):

$(this).closest('div.blog').find('h3.blog').text()
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
-1

You have extra single quote. Try to write:

function deletePost($mysqli, $title){
    $sql = "DELETE FROM blog WHERE Title =" . $title;
    mysqli_query($mysqli, $sql);
}

Also your code not protected from sql injection.

fortegente
  • 1,290
  • 2
  • 10
  • 22
  • What's wrong with the single quotes inside the string? In a SQL query, string values must be inside quotation marks. Your code will likely generated invalid SQL. – Felix Kling Nov 28 '13 at 08:20
  • $_GET['postTitle'] has single quotes. And this GET variable in sql taken in single quotes. You can see more about this http://stackoverflow.com/questions/17868387/parse-error-syntax-error-unexpected-t-encapsed-and-whitespace – fortegente Nov 28 '13 at 08:25
  • You mean the value of `$_GET['postTitle']` contains single quotes? How do you know that? And even if, it would not throw a *PHP syntax error*. – Felix Kling Nov 28 '13 at 08:27
  • If you have a close look at the question you linked to, you will notice that it is a different situation than here. The OP doesn't try to access an array in the string, they already assigned the value to `$title` before that. – Felix Kling Nov 28 '13 at 08:33
  • (1) [The OP already mentioned](http://stackoverflow.com/questions/20260800/how-to-properly-use-data-with-jquery-ajax-request/20261229#comment30222436_20260800) that the syntax error came from an `echo` statement they used for testing. (2) It would only be a problem if the OP tried to access the array inside the string, i.e. `$sql = "DELETE FROM blog WHERE Title = '$_GET['postTitle']'";` just like in the question you linked to. But that's not the case here, the OP is doing `$sql = "DELETE FROM blog WHERE Title = '$title'";`, which is different and valid. – Felix Kling Nov 28 '13 at 08:40