简体   繁体   中英

how to display the correct message?

I have a php script below known as cancelimage.php where it will display a cancel message:

<?php

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

    echo "$image_file_name Upload was Canceled";


?>

The problem is that when I click on the "Cancel" button, it does not display this message. Instead it displays the message "There was a error during file upload". I believe the reason this message is coming up instead is because I am stating "return stopImageUpload".

So what my question is that how can I return to stopImageUpload like I am doing now, but instead be able to display the cancel message instead of the message it is displaying now?

Below is my current attempt on trying to fix this but which has failed. Below shows both the cancel button function and the stopImageUpload function:

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;
}

Preface: Ajax is not my strong suit.....

You are relying on php to pull the file name... but PHP can't get the file name until the upload is initiated. You need to pull the file name from the DOM (the input button) if you want to display the file name without an actual upload taking place.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM