Solved

Hide progress loader after success

Posted on 2009-03-30
2
574 Views
Last Modified: 2012-05-06
I am working on a AJAX/JSON driven commenter and I am using jQuery's AJAX function before send i have a progress ticker pop up then when it runs i want to have it hide:

beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});
                        },
                        success: function(latest){
                              $("#loading").hide();
                              newContent = '<div id="post-'+ latest.id +'">\n';
                              newContent += '<h3>' + latest.author + '</h3>\n';
                              newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';
                              newContent += '<p>' + latest.post_content + '</p>';
                              newContent += '<hr/>';
                              $("#contents").append(newContent).fadeIn("slow");
                        }

So I have the animated GIF in #loading, so beforeSend, show the element, then on success hide it. But it seems to just keep it showing all the time unless i refresh the page. Any idea what I can do to have it hide after it sends and show the form (i know i would just fade the form back in)

Thoughts?

Ryan
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Facebook like ajax post - jQuery - ryancoughlin.com</title>

<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen, projection" />

<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />

<!--[if IE]><link rel="stylesheet" href="css/ie.css" type="text/css" media="screen, projection"><![endif]-->

<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">

/* <![CDATA[ */

$(document).ready(function(){

	var content    = "";

	var newContent = "";

	$('p.validate').hide();

	

	$.getJSON("readJSON.php",function(data){

		$.each(data.posts, function(i,post){

			content = '<div id="post-'+ post.id +'">\n';

			content += '<h3>' + post.author + '</h3>\n';

			content += '<p class="small quiet">' + post.date_added + '</p>\n';

			content += '<p>' + post.post_content + '</p>';

			content += '<hr/>';

			$("#contents").append(content).fadeIn("slow");		

		});

	});  

	

	$(".reload").click(function() { 

		$("#posts").empty();

	});

	

	$("#add_post").submit(function() {

		$('p.validate').empty();

		

		// we want to store the values from the form input box, then send via ajax below

		var author = $('#author').attr('value');

		var post   = $('#post').attr('value'); 

		

		if(($("#author").val() == "") && ($("#post").val() == "")){

			$("p.validate").fadeIn().append("Both fields are required.");

			$('#author,#post').fadeIn().addClass("error");

		}else{

			$.ajax({

				type: "POST",

				url: "ajax.php",

				data: "author="+ author + "&post=" + post,

				dataType: "json",

				beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});

				},

				success: function(latest){

					$("#loading").hide();

					newContent = '<div id="post-'+ latest.id +'">\n';

					newContent += '<h3>' + latest.author + '</h3>\n';

					newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';

					newContent += '<p>' + latest.post_content + '</p>';

					newContent += '<hr/>';

					$("#contents").append(newContent).fadeIn("slow");

				}

			});

		

	}

	return false;

	});

	

});

/* ]]> */

</script>

<style type="text/css">

h3{margin:10px 0;}

p{margin:5px 0;}

#posts{display:none;}

#loading{text-align:center;}

</style>

</head>

<body>

        <div class="container">

                <div class="span-24">

						<div id="post-container" class="span-9 colborder">

						    	<h2>Posts Loaded Via AJAX/JSON:</h2>

								<div id="contents"></div>	

						</div>

						<div id="form" class="span-11">

							<h2>New Post:</h2>

						

							<form name="add_post" id="add_post" action="">

								<label>Author:</label><br />

								<input type="text" name="author" id="author" size="15" class="text" /><br />

								<label>Post:</label><br />

								<textarea name="post" id="post" rows="5" cols="5" class="text"></textarea><br />

								<input type="submit" value="Post" id="submit" /><br />

							</form><br />

							<div id="loading" style="display:none">

							    <img src="images/loading.gif" alt="Loading" />

							</div>

							<p class="validate error"></p>

							

						</div>	

                </div>

				<div class="span-24">

					<a href="#" class="reload">Reload</a>

				</div>

        </div>

</body>

</html>

Open in new window

0
Comment
2 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 24023942
Is the call successfull?
There could be an error as well which is not handled.
You can also specify the complete function wich allways gets executed no matter if the call is successfull or not.
                        $.ajax({

                                type: "POST",

                                url: "ajax.php",

                                data: "author="+ author + "&post=" + post,

                                dataType: "json",

                                beforeSend: function(){$("#add_post").fadeOut("fast",function loadAnimation(){$("#loading").show();$("p.error").hide();});

                                },

                                success: function(latest){

                                        newContent = '<div id="post-'+ latest.id +'">\n';

                                        newContent += '<h3>' + latest.author + '</h3>\n';

                                        newContent += '<p class="small quiet">' + latest.date_added + '</p>\n';

                                        newContent += '<p>' + latest.post_content + '</p>';

                                        newContent += '<hr/>';

                                        $("#contents").append(newContent).fadeIn("slow");

                                },

                                complete: function() {

                                        $("#loading").hide();

                                }  

                        });

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 24081782
Let me give this a go! Thanks for your help.

Ryan
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Safari SVG Image Problem 1 41
Jquery form submit not working ? 7 36
form button worked now it doesnt anymore 9 41
Loop and hide/show dropdown option with jQuery 4 22
by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now