简体   繁体   English

使用按钮和ajax删除mysql

[英]delete mysql using button and ajax

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. Ajax 无法运行。

HTML/PHP 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 PHP

$id = $_POST['delete_id'];
include('functions.php');
$DB = connexion();
$DB->query('DELETE FROM videos WHERE id = "'.$id.'"');

Add location.reload() to the success function.location.reload()添加到成功函数中。

That is the syntax to use.这就是要使用的语法。

Plus, you're also open to a serious SQL injection.此外,您还可能面临严重的 SQL 注入。

Consult the following:咨询以下内容:

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

I am hoping there are rows you are deleting so make sure that you pass an id element to each row as我希望有您要删除的行,因此请确保将 id 元素传递给每一行


here goes your video 你的视频来了

and when your AJAX return success just make the element disappear当你的 AJAX 返回成功时,让元素消失

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM