0

Alright, so I asked a question yesterday regarding how to save the blog posts that a user makes. I figured out the database side of it, and that works fine. Now, I want to REMOVE a blog post based after clicking an onclick button. Through my hours of digging through the web, I've found calling an jQuery AJAX function is the best way to go about it. I've been tooling around with it, but I can't get this working.

Blog code retrieved from database in blog.php:

$connection = mysql_connect("...", "...", "...") or die(mysql_error());
$database = mysql_select_db("...") or die(mysql_error());

$query = mysql_query("SELECT * FROM template") or die(mysql_error());
$template = mysql_fetch_array($query);

$loop = mysql_query("SELECT * FROM content ORDER BY content_id DESC") or die (mysql_error());
while ($row = mysql_fetch_array($loop))
{
        print $template['Title_Open'];
        print $row['title'];
        print '<button class="deletePost" onClick="deleteRow(' . $row['content_id'] . ')">Remove Post</button>';
        print $template['Title_Close'];

        print $template['Body_Open'];
        print $row['body'];
        print $template['Body_Close'];
}

mysqli_close($connection);

This creates the following HTML on home.php:

<div class="blogtitle" class="post3">Title
<button class="deletePost" onClick="deleteRow(3)">Remove Post</button></div>
<div class="blogbody" class="post3">Content</div>

Which should call my remove.js when button is clicked (This is where I start to lose what I'm doing):

$function deleteRow(id){
    $.ajax({
        url: "remove.php",
            type: "POST",
            data: {action: id}
    });
    return false;         
};

Calling remove.php (No idea what I'm doing):

$con=mysqli_connect("...","...","...","...");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_POST['action'];

$query = mysql_query("DELETE FROM content WHERE content_id=$id") or die(mysql_error());

My goal here is to REMOVE the row with the ID from the table which would in turn remove the blog post entirely since it won't see the row when it loops through the database table.

Any ideas?

Thanks for your help, Kyle

  • Welcome to Stack Overflow! [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 May 09 '13 at 23:57
  • It's a final project for my Intro to Web Development class. He's going to be happy that I'm using database at all haha. – Nomadic Penguin May 09 '13 at 23:57
  • Wish somebody would have told me sooner that mysql_* is deprecated. It's all I could find when I was researched, thanks! – Nomadic Penguin May 10 '13 at 00:16

2 Answers2

1

couple of issues in your original code: the functions in Jquery shouldn't use a $ sign at the beginning and since you need to pass a single value I would use the query string rather than the POst, and instead of calling the "die" in php I would use the affected rows to return the callback of whether or not the value was deleted. But this is just my approach, there other ways I'm sure.

Here are little improvements in you code:

    //HTML
    <div class="blogtitle" class="post3">Title
    <button class="deletePost" data-item="3" >Remove Post</button></div>
    <div class="blogbody" class="post3">Content</div>

   //JQUERY
    jQuery(document).ready(function($) {

        $('button.deletePost').each(function(){

            var $this = $(this);

            $this.click(function(){

                var deleteItem  = $this.attr('data-item');

                $.ajax({url:'remove.php?action='+deleteItem}).done(function(data){

                    //colect data from response or custom code when success

                });
             return false;  
            });

        });


    });


   //PHP
    <?php 

    $id = $_REQUEST['action'];
    $query = mysql_query('DELETE FROM content WHERE content_id="'.$id.'"');
    $confirm = mysql_affected_rows() > 0 ? echo 'deleted' : echo 'not found or error';

     ?>

Hope this sample helps :) happy coding !

  • Thanks for the input! I'll test this out and see what I can do. – Nomadic Penguin May 10 '13 at 00:56
  • Alright, seems like we're getting somewhere. I'm getting an "500 (Internal Server Error)" at "$.ajax({url:'remove.php?action='+deleteItem}).done(function(data){". Do I need to have success code? – Nomadic Penguin May 10 '13 at 01:21
  • The success code is mostly used for two reasons: a) to notify that the call was successful and b) to create custom actions or notifications to the user that his action has been performed. – maurix suarez May 10 '13 at 06:13
  • The success is mostly used for: a) make sure the call was successful or not (debugging) and b) to create custom actions or notifications to the user that his action has been performed.the "500 error" is a "fatal error" while processing the Php. it means that there might be a syntax error/mysql call not properly configurated. Something else I noticed from you original code was "mysqli_close($connection)" , this is no necessary as the database closes itself, and make sure to verify your script when you type "mysql" and not "mysqli" they are different modules/methods. – maurix suarez May 10 '13 at 06:21
0

i hope this should help you i used this to remove items from my shopping cart project.

$(".deleteitem").each(function(e) {
            $(this).click(function(e) {
                $.post("library/deletefromcart.php",{pid:$(this).attr('prodid'), ajax:true},function(){
                    window.location.reload()
                })
                return false;
             });
        });