我有一个取消按钮,用户可以在其中取消文件上传,它会显示一条取消消息。现在我还想发生的是,当用户单击“取消”按钮时,它将在数据库中查找已取消的文件名并删除数据库行。问题是它根本没有删除数据库行。我怎样才能做到这一点。目前我正在使用 jpuery.ajax 方法,您可以在下面的代码中看到该方法。
下面是表单代码:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='button' name='imageCancel' class='imageCancel' cancel_image_file_name='" + imagefilename + "' value='Cancel' /></label></form>");
下面是取消按钮功能:
$('.imagef1_cancel').eq(window.lastUploadImageIndex).find(".imageCancel").on("click", function(event) {
var cancel_image_file_name = $(this).attr('cancel_image_file_name');
jQuery.ajax("cancelimage.php?imagefilename=" + cancel_image_file_name)
return stopImageUpload(2, cancel_image_file_name);
});
最后下面是 jquery.ajax 导航到的 cancelimage.php 脚本,据说可以删除包含文件名的数据库行:
<?php
...
//I have connected to database
$cancel_image_file_name = $_GET["imagefilename"];
$imagecancelsql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($cancel_image_file_name)."'";
mysql_query($imagecancelsql);
mysql_close();
?>
更新:
以下是当我回显删除查询时它当前显示的内容:
注意:未定义的索引:第 19 行 /web/stud/xxx/.../cancelimage.php 中的图像文件名 DELETE FROM Image WHERE ImageFile = 'ImageFiles/'
下面是删除函数的代码,当按下删除按钮时,它将导航到 deleteimage.php 脚本并删除数据库行:
function stopImageUpload(success, imagefilename){
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
$('.listImage').eq(window.lastUploadImageIndex).find(".deletefileimage").on("click", function(event) {
var image_file_name = $(this).attr('image_file_name');
jQuery.ajax("deleteimage.php?imagefilename=" + image_file_name)
$(this).parent().remove();
});
return true;
}
下面是 deleteimage.php 脚本:
<?php
//connected to DB
$image_file_name = $_GET["imagefilename"];
$imagedeletesql = "DELETE FROM Image
WHERE ImageFile = 'ImageFiles/". mysql_real_escape_string($image_file_name)."'";
mysql_query($imagedeletesql);
mysql_close();
?>
下面是取消按钮功能现在的样子的更新:
function startImageUpload(imageuploadform, imagefilename){
$('.imagef1_cancel').eq(window.lastUploadImageIndex).find(".imageCancel").on("click", function(event) {
var cancel_image_file_name_ = $(this).attr('css');
var cancel_image_file_name = '';
var style_array = cancel_image_file_name_.split(" ");
for(i=0;i<style_array.length;i++){
if(style_array[i].substr(0,2) == "__"){
cancel_image_file_name = style_array[i].slice(2,style_array[i].length-2);
}
}
jQuery.ajax("cancelimage.php?imagefilename=" + cancel_image_file_name)
return stopImageUpload(2, cancel_image_file_name);
});
return true;
}
我将按钮输入标签更改为取消按钮:
<input type='button' name='imageCancel' class='imageCancel __"+ imagefilename + "' value='Cancel' />