如何显示正确的消息?

时间:2012-05-13 22:09:24

标签: php javascript jquery ajax

我有一个名为cancelimage.php的php脚本,它会显示一条取消消息:

<?php

$image_file_name = $_FILES['fileImage']['name'] ;

    echo "$image_file_name Upload was Canceled";


?>

问题是,当我点击“取消”按钮时,它不会显示此消息。而是显示消息“文件上载期间出错”。我相信这条消息的出现是因为我说的是“return stopImageUpload”。

那么我的问题是我怎么能像现在这样回到stopImageUpload,而是能够显示取消消息而不是现在显示的消息?

以下是我目前尝试修复此问题但尝试失败的尝试。下面显示了取消按钮功能和stopImageUpload功能:

var cancelimagecounter = 0;
function startImageUpload(imageuploadform, imagefilename) {
    cancelimagecounter++;
    var _cancelimagecounter = cancelimagecounter;
    $('.imageCancel').on("click", function (event) {
        $('.upload_target').get(0).contentwindow
        $("iframe[name='upload_target']").attr("src", "javascript:'<html></html>'");
        jQuery.ajax("cancelimage.php?fileImage=" + image_file_name).done(function (data) {
            $(".imageemsg" + _cancelimagecounter).html(data);
        });
        return stopImageUpload();
    });
    return true;
}
var imagecounter = 0;
function stopImageUpload(success, imagefilename) {
    var result = '';
    if (success == 1) {
        result = '<span class="imagemsg' + imagecounter + '">The file was uploaded successfully!</span><br/><br/>';
        $('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
    } else {
        result = '<span class="imageemsg">There was an error during file upload!</span><br/><br/>';
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

前言:Ajax不是我的强项.......

您依靠php来提取文件名...但是在启动上传之前,PHP无法获取文件名。如果要在没有实际上传的情况下显示文件名,则需要从DOM(输入按钮)中提取文件名。

$('input:file').change(function(){
    if ($(this).val()) {
    var filename = $(this).val().replace('C:\\fakepath\\', '');
    }
    $('.imageemsg').html('<p>this is the filename' + filename + '</p>');
});