1

I want to delete files in my database instantly when I press a button. Files were deleted but I am obliged to refresh my navigator. Ajax doesn't operate.

HTML/PHP

  echo '<button class="delete_video" id="'.$videoId.'" type="button">Delete</button>';

javaScript

   $(document).ready(function()
    {
        $(".delete_video").click(function()
        {
            var del_id = $(this).attr('id');
            $.ajax({
                type:'POST',
                url:'delete.php',
                data:'delete_id='+del_id,
                success: function(data)
                {
                    //confirmation of deletion
                }
            });
        });
    });

PHP

$id = $_POST['delete_id'];
include('functions.php');
$DB = connexion();
$DB->query('DELETE FROM videos WHERE id = "'.$id.'"');
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 4
    You said the files were deleted, so what is the problem? Refreshing the page after deleting? If so then just add location.reload() to the success function. – jussius May 27 '16 at 12:07
  • 1
    Yes, the refreshing is the problem. Thank's you solded the problem. –  May 27 '16 at 12:09
  • 1
    Nadir; I noticed you accepted an answer below and there is no explanation given for it, it's just "drop-in" code and probably based on the comment given up here. You should have told/asked @jussius to submit an answer for it, since that person was the first one to give you the solution; I feel it's only fair. – Funk Forty Niner May 27 '16 at 12:29
  • You're also open to a serious SQL injection @Nadir – Funk Forty Niner May 27 '16 at 12:31
  • @jussius I suggest you post an answer for this and not just "drop-in" code like the answer given originally. I'm sure Nadir will accept it. ;-) – Funk Forty Niner May 27 '16 at 12:41
  • An injection ? Wow, I don't know the issue of this problem but thank you for having informed me. –  May 27 '16 at 12:41
  • @Nadir Yes. Read the following http://php.net/manual/en/security.database.sql-injection.php and http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php they will help you. Using a prepared statement is important. and you're welcome. – Funk Forty Niner May 27 '16 at 12:43
  • You're welcome Nadir, *cheers* – Funk Forty Niner May 27 '16 at 12:44
  • @Nadir further to my comment; you can also just do `$id = (int)$_POST['delete_id'];` if the id is an integer/number. However, using a prepared statement in conjunction with this, will be even better. – Funk Forty Niner May 27 '16 at 12:48
  • I posted it as an answer. Also, what Fred said about injections is true, you should use parameterized queries. – jussius May 27 '16 at 12:53

3 Answers3

1

Add location.reload() to the success function.

That is the syntax to use.

Plus, you're also open to a serious SQL injection.

Consult the following:

Community
  • 1
  • 1
jussius
  • 3,114
  • 15
  • 21
0
$(document).ready(function()
    {
        $(".delete_video").click(function()
        {
            var del_id = $(this).attr('id');
            $.ajax({
                type:'POST',
                url:'delete.php',
                data:'delete_id='+del_id,
                success: function(data)
                {
                    //reload page
                    location.reload();
                }
            });
        });
    });
Ranjeet Singh
  • 588
  • 5
  • 12
0

I am hoping there are rows you are deleting so make sure that you pass an id element to each row as


here goes your video

and when your AJAX return success just make the element disappear

 <div id="del_id">
      here goes your video to delete
 </div>


$(document).ready(function()
{
    $(".delete_video").click(function()
    {
        var del_id = $(this).attr('id');
        $.ajax({
            type:'POST',
            url:'delete.php',
            data:'delete_id='+del_id,
            success: function(data)
            {
                jQuery('#del_id').fadeOut('slow');
            }
        });
    });
});
Sandeep Pujare
  • 147
  • 2
  • 11