Skip to content
Advertisement

Deleted images still being shown

For some reason, my website still showing images that were already deleted from the specified folder and I have no idea why that’s happening and how to solve that.

Process: When the button to delete all admins is pressed, it calls a PHP function that truncate the tables administration, adminimg and login, delete all images from a folder related to id’s on table administration with unlink(), and create a registry on administration table with id=1(auto_increment) and name=”abc”.

Problem: I have a jQuery function that display a specific admin information on textboxes, verify the value in the textbox for the adminID, and display the image associated to that id. After executing the process above, when i call the jQuery function, it display correctly the id=1 and name=”abc” but shows the deleted image associated to the admin with id=1 before truncate the tables.

jQuery function (if necessary)

$(".btneditadmin").click( e =>{
  let textvalues = displayDataAdmin(e);
  let id = $("input[name*='idadmin']");
  let name = $("input[name*='nameadmin']");

  id.val(textvalues[0]);
  nome.val(textvalues[1]);

  var img_url = 'Images/Administration/admin'+$("#idadmin").val()+'.jpg';
  $("#admin-image").attr('src',img_url);
});

function displayDataAdmin(e) {
  let id = 0;
  const td = $("#tbody tr td");
  let textvalues = [];

  for (const value of td){
      if(value.dataset.id == e.target.dataset.id){
         textvalues[id++] = value.textContent;
      }
  }
  return textvalues;
}

Advertisement

Answer

If you’re sure that image isn’t there anymore then it’s caching issue and something like this would take care of it

let img_url = 'Images/Administration/admin'+$("#idadmin").val()+'.jpg';
img_url += '?' + new Date().getTime() ; // cache killer
$("#admin-image").attr('src', img_url);

However, you’re calling that function no matter what so I would suggest a onload/error check

​$('#admin-image').load(function(){ // when loaded successfully
   console.log('success');  
}).error(function(){  // when theres an error
   $(this).remove() 
    // or you could replace it with a default image  
   $(this).attr('src', '/images/default.jpg');
});​​​​​
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement