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 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥20 关于变压器的具体案例分析