admin管理员组文章数量:1329105
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.
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.
Share Improve this question asked Mar 20, 2014 at 12:05 SCCSCC 871 gold badge3 silver badges9 bronze badges 2- @SCC can you confirm if the Ajax call is succeding by writing a log entry in the very begining of the PHP script? I want to make sure if the Ajax call is succeding. If it succeeds then we can inspect just the PHP script otherwise we have question the java script itself. – Siva Senthil Commented Mar 20, 2014 at 12:07
- Did you check if you are getting any Javascript errors? – Lepanto Commented Mar 20, 2014 at 12:14
4 Answers
Reset to default 6Try with this
$.ajax({
type: "POST",
url: "deleteimage.php",
data: {"imagefile":imagefile},
success: function(response) {
// do something
}
});
$.ajax({
type: "POST",
url: "deleteimage.php",
data: {imagefile: imagefile}, // here
success: function(response) {
// do something
}
});
Change
data: imagefile,
To
data: {imagefile, imagefile },
$.ajax({
type: "POST",
url: "deleteimage.php",
data: "imagefile=" + imagefile,
success: function(response) {
// do something
}
});
本文标签: javascriptDelete image file using ajax callStack Overflow
版权声明:本文标题:javascript - Delete image file using ajax call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742231540a2437306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论