Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-07-2012, 01:42 PM   PM User | #1
AdvaMax
New to the CF scene

 
Join Date: Sep 2012
Location: South Africa
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
AdvaMax is an unknown quantity at this point
Ajax preloader image

Hi guys,

I am still new at Javascript, and have some difficulty getting a preloader image to display in my existing code.

Basically the user submits an identity number which is sent to another php file for verification, and the result is displayed without refreshing the page.

For this I came accross this Ajax code:

function submitForm() {
$.ajax({type:'POST', url: 'verify.php', data:$('#ValidationForm').serialize(), success: function(response) {
$('#ValidationForm').find('.form_result').html(response);
}});

return false;
}


Here is the form code:
<form id="ValidationForm" onsubmit="return submitForm();">
<input type="text" name="number">
<input type="Submit" value="Verify">
</form>

How can I amend the above code to have a preloader image (or text) shown while the user waits for the result?

I have read about the readystate event, but my attempts to use it in the JavaScript above failed.

Help will be greatly appreciated.

Thanks
AdvaMax is offline   Reply With Quote
Old 09-07-2012, 04:24 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
this assumes your loading image or container element has a class of loaderimg
- also this only happens upon a successful ajax request, you should also consider using the error: setting as well - if you havent already ofc ourse

Code:
function submitForm() {
$('.loaderimg').fadeIn();
$.ajax({type:'POST', url: 'verify.php', data:$('#ValidationForm').serialize(), success: function(response) {
$('#ValidationForm').find('.form_result').html(response);
$('.loaderimg').fadeOut();
}});

return false;
}
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users

Last edited by DanInMa; 09-07-2012 at 04:27 PM..
DanInMa is offline   Reply With Quote
Old 09-08-2012, 11:10 AM   PM User | #3
AdvaMax
New to the CF scene

 
Join Date: Sep 2012
Location: South Africa
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
AdvaMax is an unknown quantity at this point
Thanks DanInMa,

Solution was relative simple, but since I didn't know better, you directed me in the right direction.

I added a div with the loader image in the container element, and made it not visible:

Quote:
<div id='loading' style='display: none;'><img src='loader.gif'></div>
Then I did the following in the javascript (make the loading div visible once the form is submitted):

Quote:
function submitForm() {
var loadingdiv = document.getElementById('loading');
loadingdiv.style.display = "block";
$.ajax({type:'POST', url: 'verify.php', data:$('#ValidationForm').serialize(), success: function(response) {
$('#ValidationForm').find('.form_result').html(response);
}});

return false;
}
Now since the container element gets replaced with all new info, it's not necessary for me to make the loading div invisible again.

And it works perfect!
AdvaMax is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 AM.


Advertisement
Log in to turn off these ads.