I am working on a product posting page, which supports up to 5 images. I set the 5 images limit with data-max_length=”5″.
For the Add Page, I use the following:
HTML:
<div class="upload__box">
<div class="upload__btn-box">
<label class="upload__btn">
<p>Upload images</p>
<input type="file" multiple="" data-max_length="5" class="upload__inputfile">
</label>
</div>
<div class="upload__img-wrap"></div>
</div>
CSS:
html * {
box-sizing: border-box;
}
p {
margin: 0;
}
.upload {
&__box {
padding: 40px;
}
&__inputfile {
width: .1px;
height: .1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
&__btn {
display: inline-block;
font-weight: 600;
color: #fff;
text-align: center;
min-width: 116px;
padding: 5px;
transition: all .3s ease;
cursor: pointer;
border: 2px solid;
background-color: #4045ba;
border-color: #4045ba;
border-radius: 10px;
line-height: 26px;
font-size: 14px;
&:hover {
background-color: unset;
color: #4045ba;
transition: all .3s ease;
}
&-box {
margin-bottom: 10px;
}
}
&__img {
&-wrap {
display: flex;
flex-wrap: wrap;
margin: 0 -10px;
}
&-box {
width: 200px;
padding: 0 10px;
margin-bottom: 12px;
}
&-close {
width: 24px;
height: 24px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 10px;
right: 10px;
text-align: center;
line-height: 24px;
z-index: 1;
cursor: pointer;
&:after {
content: '2716';
font-size: 14px;
color: white;
}
}
}
}
.img-bg {
background-repeat: no-repeat;
background-position: center;
background-size: cover;
position: relative;
padding-bottom: 100%;
}
JavaScript:
jQuery(document).ready(function () {
ImgUpload();
});
function ImgUpload() {
var imgWrap = "";
var imgArray = [];
$('.upload__inputfile').each(function () {
$(this).on('change', function (e) {
imgWrap = $(this).closest('.upload__box').find('.upload__img-wrap');
var maxLength = $(this).attr('data-max_length');
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
var iterator = 0;
filesArr.forEach(function (f, index) {
if (!f.type.match('image.*')) {
return;
}
if (imgArray.length > maxLength) {
return false
} else {
var len = 0;
for (var i = 0; i < imgArray.length; i++) {
if (imgArray[i] !== undefined) {
len++;
}
}
if (len > maxLength) {
return false;
} else {
imgArray.push(f);
var reader = new FileReader();
reader.onload = function (e) {
var html = "<div class='upload__img-box'><div style='background-image: url(" + e.target.result + ")' data-number='" + $(".upload__img-close").length + "' data-file='" + f.name + "' class='img-bg'><div class='upload__img-close'></div></div></div>";
imgWrap.append(html);
iterator++;
}
reader.readAsDataURL(f);
}
}
});
});
});
$('body').on('click', ".upload__img-close", function (e) {
var file = $(this).parent().data("file");
for (var i = 0; i < imgArray.length; i++) {
if (imgArray[i].name === file) {
imgArray.splice(i, 1);
break;
}
}
$(this).parent().parent().remove();
});
}
The Add Page works pretty well.
However, after a product is added and the images are uploaded, for the Edit Page, I have a problem.
I use the following code to show the uploaded images’ preview.
HTML and PHP:
<?php if($row_item["image_1"] != '') { ?>
<div class='upload__img-box'><div style='background-image: url(./images/<?= $row_item["image_1"] ?>)' data-number='1' data-file='' class='img-bg'><div class='upload__img-close'></div></div></div>
<?php } ?>
<?php if($row_item["image_2"] != '') { ?>
<div class='upload__img-box'><div style='background-image: url(./images/<?= $row_item["image_2"] ?>)' data-number='2' data-file='' class='img-bg'><div class='upload__img-close'></div></div></div>
<?php } ?>
<?php if($row_item["image_3"] != '') { ?>
<div class='upload__img-box'><div style='background-image: url(./images/<?= $row_item["image_3"] ?>)' data-number='3' data-file='' class='img-bg'><div class='upload__img-close'></div></div></div>
<?php } ?>
<?php if($row_item["image_4"] != '') { ?>
<div class='upload__img-box'><div style='background-image: url(./images/<?= $row_item["image_4"] ?>)' data-number='4' data-file='' class='img-bg'><div class='upload__img-close'></div></div></div>
<?php } ?>
<?php if($row_item["image_5"] != '') { ?>
<div class='upload__img-box'><div style='background-image: url(./images/<?= $row_item["image_5"] ?>)' data-number='5' data-file='' class='img-bg'><div class='upload__img-close'></div></div></div>
<?php } ?>
The problem now is, in the Edit Page, the 5 images limit doesn’t work properly.
For example, let’s say in the Add Page, the user uploaded 2 images. Now, in the Edit Page, the user should only be able to add a maximum of 3 images only. But this doesn’t work.
Also, if the user removed an image, then the user should be allowed to add an additional 4 images.
Please help me, how can I make the Edit Page work?