I have a piece of code which by determining on wether an upload is successsful, cancelled or unsuccessful it displays the relevant messages:
function stopImageUpload(success, imagefilename){
if (success == 1){
result = 'The file was uploaded successfully';
$('.listImage').eq(window.lastUploadImageIndex).append('
}
else if (success == 2){
result = ' The file upload was cancelled';
}
else {
result = 'There was an error during file upload';
}
});
Now depending on the outcome of the upload I am trying to determine when each message should appear:
Current code of the iframe load:
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow;
stopImageUpload(2);
$("iframe[name='upload_target_image']").on("load",function() {
stopImageUpload(1);
}).attr("src", "cancelimage.php");
Original code of the iframe load:
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow;
$("iframe[name='upload_target_image']").on("load",function() {
stopImageUpload(2);
}).attr("src", "cancelimage.php");
What happended with the orignal code was that when a file uploaded succesfully, what happended was that a split second it displayed the success == 1 message but then straight away it displayed the cancel message which is success -- 2.
This was obviously incorrect so to try to fix it, I tried the current code solution so that what I thought would happen is that if cancelled, display the success == 2 message, if file was successful, it displays the success == 1 message and that message did not change.
The problem though is that the current code is providing a error below on this line while the orignal code never provided this error:
function htmlEncode(value) { return $('
The error displaying is:
typeError: $(...).text(...).html is not a function
My question is how can this error be fixed? I am assuming I need to change code in iframe load to prevent this.
UPDATE:
HTML:
Image File:
" +
JQuery:
var sourceImageForm;
function htmlEncode(value) { return $('
//Function for when file starts uploading
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
$(imageuploadform).find('.imagemsg').css('visibility','hidden');
sourceImageForm = imageuploadform;
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow;
stopImageUpload(2);
$("iframe[name='upload_target_image']").on("load",function() {
stopImageUpload(1);
}).attr("src", "cancelimage.php");
});
return true;
}
var imagecounter = 0;
//Function for after upload has stopped
function stopImageUpload(success, imagefilename){
var result = '';
imagecounter++;
if (success == 1){
result = 'The file was uploaded successfully';
$('.listImage').eq(window.lastUploadImageIndex).append('
}
else if (success == 2){
result = ' The file upload was cancelled';
}
else {
result = 'There was an error during file upload';
}
$(sourceImageForm).find(".fileImage").replaceWith("");
var _imagecounter = imagecounter;
$('.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)
.done(function(data) {
$(".imagemsg" + _imagecounter).html(data);
});
$(this).parent().remove();
});
return true;
}
//clickHandler function which handle the submit when upload button is click
function imageClickHandler(imageuploadform){
if(imageValidation(imageuploadform)){
window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform);
return startImageUpload(imageuploadform);
}
return false;
}
PHP (uploads file server side):
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
if ($_FILES['fileImage']['error'] === UPLOAD_ERR_OK) {
$result = 0;
if (getimagesize($_FILES['fileImage']['tmp_name'])) {
if ((($_FILES["fileImage"]["type"] == "image/gif") || ($_FILES["fileImage"]["type"] == "image/jpeg") || ($_FILES["fileImage"]["type"] == "image/pjpeg") || ($_FILES["fileImage"]["type"] == "image/jpg")) && ($_FILES['fileImage']['size'] > 0)) {
if (is_file("ImageFiles/" . $_FILES['fileImage']['name'])) {
$parts = explode(".", $_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".", $parts);
$n = 2;
while (is_file("ImageFiles/" . $base . "_" . $n . "." . $ext))
$n++;
$_FILES['fileImage']['name'] = $base . "_" . $n . "." . $ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
else {
move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
}
}
} else {
echo "Upload was not successful";
}
?>
echo $result;
?>, '<?php
echo $_FILES['fileImage']['name'];
?>');