I have the following code which displays the image previews. But when I select new images or say when I change images then on change the previously selected images preview should be removed and only new images should show up in the preview. But its not happening. Currently, if I select 3 images then the preview shows up. But when I select more images then the preview adds up to the previous images.
Also please tell me how can I get values of <img id="imgs">
to insert in the database rather than the values from <input type="file" name="files[]" multiple="multiple" id="file-5">
. Please help.
HTML:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server" enctype="multipart/form-data">
<textarea name="status" class="textarea newstatuscontent" placeholder="What are you thinking?"></textarea>
<input type="file" name="files[]" multiple="multiple" id="file-5"><br />
<img id="imgs"><br />
<input type="submit" name="post" value="Post" class="post-btn" id="submit" />
</form>
Jquery:
$(document).ready(function(){
$("#file-5").on('change',function() {
var fileList = this.files;
for(var i = 0; i < fileList.length; i++){
var t = window.URL || window.webkitURL;
var objectUrl = t.createObjectURL(fileList[i]);
$('.removeimg').fadeIn();
$('#imgs').append('<!--span class="img_'+i+'" onclick="del('+i+')" style="cursor:pointer; margin-right: 3px;"><b>x</b></span--><img class="img_'+i+'" src="' + objectUrl + '" width="150" height="150" style="margin-right: 3px;">');
j = i+1;
if(j % 3 == 0)
{
$('#imgs').append('<br>');
}
}
});
});
Ajax code used in insert the data into the database:
$(function() {
$("#submit").click(function() {
$(this).val("Please wait...");
var textcontent = $(".newstatuscontent").val();
/*if(media == ''){*/
if(textcontent == ''){
$('.cap_status').html("Status cannot be empty. Please write something.").addClass('cap_status_error').fadeIn(500).delay(3000).fadeOut(500);
$("#submit").val("Post");
}else{
/*}else{*/
$.ajax({
type: "POST",
url: "post-status.php",
data: {content:textcontent},
cache: true,
success: function(html){
$("#shownewstatus").after(html);
$(".newstatuscontent").val('');
$("#submit").val("Post");
}
});
}
//}
return false;
});
});