The jQuery library has some functions that would be a great help to accomplish what you want. Without jQuery, you can still do this, but jQuery will simplify the process.
Here is an example similar to what I use in my own code. Here, Script.php refers to the script on your server, original.gif refers to the image on your page, and loading.gif refers to the 'loading bar' gif image.
You'll need to load the jQuery library into the page to use this code.
$(document).ready(function() {
$('#gifImage').click(gifImage_click);
// Here, gifImage is the ID of the image you want to replace with a loading gif.
});
function gifImage_click() {
// This will replace the gif image.
$('#gifImage').attr('src','loading.gif');
function successFunction(response) {
// This function executes when the script runs successfully.
$('#gifImage').attr('src','original.gif');
alert('script completed successfully');
}
function errorFunction(xhr) {
// This function executes when the script fails to execute.
$('#gifImage').attr('src','original.gif');
alert('script execution failed');
}
$.ajax({
type: "POST",
url: "Script.php",
success: successFunction,
error: errorFunction
}); // end - $.ajax
}