文件名未定义
file name is undefined
对,这就是我想要的解决方案。当用户单击"取消"按钮时,它将尝试查找要取消的文件的文件名,然后删除包含该文件名的数据库行。
问题是它无法识别文件名,因为它指出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 脚本,当用户单击"取消"按钮时,它将导航到此脚本,这是脚本用于通过使用 $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();
?>
以下是查询的打印:
注意:未定义的索引:imagecancelname in/web/stud/xxx/Mobile_app/cancelimage.php 第 19 行array(0( { } 从图像中删除,其中图像文件 = '图像文件/'
完成上传之前,图像不应该存储在您的数据库中。如果图像被取消,那么它将不会在数据库中。
要回答未定义的确切问题,在jquery中,attr是获取标记属性。( ).
您应该在文件上传输入上放置一个 id:
<input id='fileImage' name='fileImage' type='file' class='fileImage' />
然后使用它来获取值
var image_cancel = $('#fileImage').val();
我的猜测是浏览器不喜欢您在按钮上定义的自定义属性来保存文件路径。但是,您可以轻松地将此信息存储在标准属性中 - id
.试试这个 JS:
function startImageUpload(imagecancelname){
// All this can be chained into one since .html() returns the correct jQuery object
$('.imagef1_cancel').eq(window.lastUploadImageIndex).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;
}
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- Jquery未定义函数正在停止其他操作
- AngularJS指令出错-无法读取属性'编译'的未定义
- 引用错误: 未定义文件名
- 文件名未定义
- 我得到一个未定义而不是文件名
- 它一直显示未定义的文件名