Nadir Nadir - 6 months ago 23
MySQL Question

delete mysql using button and ajax

I want to delete files in my database instantly when I put press button. Files were delete but I am obliged to refresh 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.'"');

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