I've been reading the answers on this questions but I can't figure it out. Mostly because I know zero AJAX and javascript.
I have a page with a button, when the user clicks that button... a php script scans a folder in the server and do all kinds of stuff with a database.
I want to show a loading gif while this happens. The user doesn't send information or receive anything, just clicks on a button.
My first try was to send the user to the php file... which is something like this (just an example). And put a div that shows an image while the DOM loads. But that doesn't work, in fact, it doesn't even show a blank page, I can still see the page in which I click the button and, because the end of the php script has a redirection, I only see the loading icon of the browser as indicator of something happening.
<html>
<head>
<title></title>
<script type="text/javascript">
$(window).load(function() {
$(".loader").fadeOut("slow");
})
</script>
<style type="text/css">
.loader {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url('images/page-loader.gif') 50% 50% no-repeat rgb(249,249,249);
}
</style>
</head>
<body>
<div class="loader"></div>
<!-- PHP SHOULD DO STUFF HERE THEN REDIRECT BACK TO WHERE THE BUTTON WAS CLICKED-->
</body>
</html>
I understand that I have to separate the php part from the "view" part. So my question is... how do a I make the button show a loading gif... while in some way I run the php script from server-side.
I've also tried:
<script type="text/javascript">
$('#buttonid').click(function(){
$('.loader').show();
$.ajax({
type: 'POST',
url: 'script.php',
success:function(result){
$('.loader').hide();
}
}
</script>
But it's not working either. I disabled the redirection on the script and when it finishes running it takes me to that page instead of remaining on the one I made the call from.