beforeSend for .post in AJAX

phpbeforesend is really useful when you need to show some spinner while the data is being submitted and the response from the server is awaited. However I found it hard to find any set protocol to do that for .post function for AJAX in jQuery. This beforesend function does work for AJAX protocol but not for $.post.

So how to do it? Well the trick is simple. Simply use the .html at the beginning of the function when the .click function is triggered. Look at the following code for an idea:

$(‘#click’).click(function(){

$( “#messagediv” ).html( ‘<div id=”message” style=”padding-left:100px;font-weight:bold;”>Please Wait . . .</div>’ );

This will replace the old content from the div or will show any message you want to be shown in the div and then once it is complete you can simply use the function like this in the end to show the content on completion:

function() {
$(‘#messagediv’).html(“<div id=’message’ style=’padding-left:100px’></div>”);
$(‘#message’).html(“<h2>Database has been Updated</h2>”)
.hide()
.fadeIn(1500, function() {
});

Hope it helps.

 

Leave a Reply

Your email address will not be published.