doubaben7394 2014-03-20 12:05
浏览 31
已采纳

使用ajax调用删除图像文件

I have a list of images, each with different id. When user clicks on the specific <img> follows by "Delete" button, I want to remove the that <img>and also remove the image file from the server. I have the following ajax call which will achieve this purpose.

$(document).ready(function () {

    $("img").click(function () {
        var img_id = $(this).attr("id");
        var imagefile = img_id;
        img_id = "#" + img_id;

        $("button").click(function () {          // delete button
            $("#image " + img_id).remove();
            $("button").unbind();

            $.ajax({
                type: "POST",
                url: "deleteimage.php",
                data: imagefile,                        
                success: function(response) {
                // do something
                }
        }); 
    });
});

I need to know the image id before I can delete the file. So I pass this into a variable then send it over to the following php file.

<?php
    include('config.php');

    if(isset($_POST['imagefile']))
    {
        $imagefile = $_POST['imagefile'];
        $imagepath = "Users/mp4_thumbnails-".$imagefile.".jpg"; 

        unlink($imagepath);
    }
?>

Sadly this is not working. This is my first try on ajax call. Any inputs will be greatly appreciated.

  • 写回答

4条回答 默认 最新

  • doutu7123 2014-03-20 12:06
    关注

    Try with this

    $.ajax({
                    type: "POST",
                    url: "deleteimage.php",
                    data: {"imagefile":imagefile},                        
                    success: function(response) {
                    // do something
                    }
            }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题