我有一个图像列表,每个图像都有不同的 ID。当用户点击特定的 <img>
时接下来是“删除”按钮,我想删除那个 <img>
并从服务器中删除图像文件。我有以下 ajax 调用将实现此目的。
$(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
}
});
});
});
在删除文件之前,我需要知道图像 ID。所以我将其传递到一个变量中,然后将其发送到以下 php 文件。
<?php
include('config.php');
if(isset($_POST['imagefile']))
{
$imagefile = $_POST['imagefile'];
$imagepath = "Users/mp4_thumbnails-".$imagefile.".jpg";
unlink($imagepath);
}
?>
遗憾的是,这不起作用。这是我第一次尝试 ajax 调用。任何意见将不胜感激。
最佳答案
试试这个
$.ajax({
type: "POST",
url: "deleteimage.php",
data: {"imagefile":imagefile},
success: function(response) {
// do something
}
});
关于javascript - 使用ajax调用删除图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22532468/