I made a code in which the user can upload some images from a zip code. On the next page, I need to show all the images separately in a frame of 85 * 85 px.
The problem is that downloading images may take some time. Therefore, I want to show the gif of the download while the user is waiting for the image to load.
I set the src of the images as the loading gifs, while I created some checkboxes with the real source as id
echo "<td><img src=\"beelden/ajax-loader-black-16.png\" id=\"img".$image."\" style=\" width: 85px; height: 85px; border: 1px solid gray; background-color: #fff; padding: 10px;\">";
echo "<input type=\"checkbox\" id=\"img[".$image."]\" name=\"check_image[]\" value=\"".$filename."\" /></td>";
<input type="hidden" name="aantal" id="aantal" value="<?=$image?>" >
Then I created some javascript to check if the image is loaded, and when it is, it should replace the source of the image.
<script>
var aantal = document.getElementById("aantal").value;
for(var i = 0; i < aantal; i++){
var source = document.getElementById("img["+i+"]").value;
var img = new Image();
img.onload = function(){
$("#img"+i).attr('src', source);
}();
img.src = source;
}
</script>
, , , , . , ?