对,这就是我想要的解决方案。当用户单击“取消”按钮时,它会尝试查找要取消的文件的文件名,然后删除包含该文件名的数据库行。
问题是它没有识别文件名,因为它声明imagecancelname=undefined
. 由于这是未定义的,因此它无法删除包含文件名的数据库行,因为它无法识别文件名。通过执行打印 $imagecancelsql 在我取消上传文件后,它会在下面显示:
DELETE FROM Image WHERE ImageFile = 'ImageFiles/undefined'
所以问题是我怎样才能imagecancelname
识别将要被取消的文件的名称,然后它可以用来删除包含文件名的数据库行?
以下是当前的表单代码:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(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'></p>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
下面是 startImageUpload 函数,它在文件上传时显示取消按钮,如果用户单击取消按钮,它包含取消按钮功能:
var sourceImageForm;
function startImageUpload(imageuploadform, imagecancelname){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
sourceImageForm = imageuploadform;
/*The above would show and hide elements within the form the file is being uploaded.
For example if I have three forms and the second form is uploading a file, then it
shows and hides the elements within the second form only */
$(imageuploadform).find('.imagef1_cancel').html('<div><button type="button" class="imageCancel" id="image_cancel_' + imagecancelname + '">Cancel</button></div>').find(".imageCancel").on("click", function(event) {
$.ajax("cancelimage.php?imagecancelname=" + $(this).attr('id').slice(13));
});
return true;
}
最后是cancelimage.php 脚本,当用户单击Cancel 按钮时,它将导航到该脚本,该脚本用于通过使用$GET 方法查找文件名来删除数据库行。
<?php
// ... connected to DB
$image_cancel_ = $_GET["imagecancelname"];
$imagecancelsql = "
DELETE FROM Image
WHERE ImageFile = 'ImageFiles/".mysql_real_escape_string($image_cancel_)."'
";
print $imagecancelsql;
mysql_close();
?>
以下是查询的打印:
注意:未定义索引:第 19 行 /web/stud/xxx/Mobile_app/cancelimage.php 中的 imagecancelname array(0) { } DELETE FROM Image WHERE ImageFile = 'ImageFiles/'