<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Developer Plus &#187; AJAX</title>
	<atom:link href="http://webdeveloperplus.com/category/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://webdeveloperplus.com</link>
	<description>The Ultimate Web Developer&#039;s Resource</description>
	<lastBuildDate>Sun, 04 Jul 2010 11:38:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>New WordPress Plugin: WDP AJAX Comments</title>
		<link>http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/</link>
		<comments>http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 13:28:57 +0000</pubDate>
		<dc:creator>Satbir</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[ajax comments]]></category>
		<category><![CDATA[wordpress plugins]]></category>

		<guid isPermaLink="false">http://webdeveloperplus.com/?p=242</guid>
		<description><![CDATA[Today, I am proud to release my first WordPress plugin named &#8220;WDP AJAX Comments&#8221;. This plugin will enable AJAX Comments on your WordPress Blog and enhance the user experience using nifty on page comment form submission and validation of input before submission. Features Enable AJAX Commenting Client Side Form Validation Easily rolls back if JS [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I am proud to release my first WordPress plugin named <strong>&#8220;WDP AJAX Comments&#8221;</strong>. This plugin will enable <a href="http://webdeveloperplus.com/wordpress/how-to-ajaxify-wordpress-comment-posting/">AJAX Comments</a> on your WordPress Blog and enhance the user experience using nifty on page comment form submission and validation of input before submission.<br />
<span id="more-242"></span></p>
<h3>Features</h3>
<ul>
<li>Enable AJAX Commenting</li>
<li>Client Side Form Validation</li>
<li>Easily rolls back if JS disabled</li>
<li>Easily configure custom styles for messages</li>
</ul>
<h3>Download</h3>
<p><a href="http://wordpress.org/extend/plugins/wdp-ajax-comments/" ><strong>Download the Plugin</strong></a> from official WordPress Plugin repository.</p>
<h3>Installing &#038; Activating</h3>
<p>Here&#8217;s how to install and use this plugin to its full potential:<br />
1. Firstly, go to <em>Add New Plugin</em> in your WordPress admin panel, and search for <em>wdp ajax comments</em> and you&#8217;ll see the listing, install the plugin by clicking on <em>&#8216;Install&#8217;</em>. Or you can go to <a href="http://wordpress.org/extend/plugins/wdp-ajax-comments/" target="_blank">WordPress plugin repository page</a> and download the plugin and extract it in your <em>wp-content/plugins</em> folder.<br />
2. Now activate this plugin, and your blog is ready with AJAX comment posting, but one thing you still need to do is enabling the client side comment form validation.<br />
3. To enable form validation, refer to the below section.</p>
<h3>Enabling Comment Form Validation</h3>
<p>This plugin makes use of jQuery Validation plugin to validate comment form, so you will need to add some css classes to your comment form. Here&#8217;s how to do that:<br />
1. Open up your theme&#8217;s <strong>comments.php</strong> file in an editor.<br />
2. Find out the code lines where comment form starts.<br />
3. Now, you need to add following CSS classes to comment form inputs:</p>
<ul>
<li>To comment author name input, add <code>class="required"</code></li>
<li>To comment author email input, add <code>class="required email"</code></li>
<li>To comment author URL input, add <code>class="url"</code></li>
<li>To the comment textarea, add <code>class="required"</code></li>
</ul>
<p><img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/ajax-comments-wordpress.png" alt="modify comment form" title="modify comment form" width="635" height="202" class="largepostimage" /></p>
<p>4. Save the file and now your comment form validation will work.</p>
<h3>Styling The Messages</h3>
<p>WDP AJAX Comments provides you with full control over the looks of various messages using CSS. To define your own styles, create a new stylesheet file named <em>wdp-ajax-styles.css</em> in your theme&#8217;s folder and define styles for these classes:</p>
<ul>
<li><code>.wdpajax-error</code> This class is used for the message shown when AJAX error is encountered.</li>
<li><code>.wdpajax-success</code> This class is used for success message shown after successful comment submission.</li>
<li><code>.wdpajax-loading</code> This class is used to show the &#8216;Processing..&#8217; message while performing AJAX request. You can add animated loading gif&#8217;s as background to give a nice AJAX effect.</li>
<li><code>label.error</code> This class is used to show validation errors.</li>
</ul>
<p>If your theme folder does not contain <em>wdp-ajax-styles.css</em> file, the default stylesheet of the plugin will be used.</p>
<h3>Demo</h3>
<p>If you would like to see the demo, go to the <a href="http://demo.webdeveloperplus.com/wordpress/" title="View Demo" target="_blank" ><strong>demo page</strong></a>.</p>
<h3>What to Expect in Coming Versions</h3>
<p>Currently, this plugin just AJAXifies comment submission and does not update the comments displayed automatically. So, this is one of the feature you should expect in coming editions. Another feature i have thought is giving you control over the messages displayed on AJAX requests.</p>
<p>If you would like to suggest a new feature, feel free to comment below, just prepend your comment with <strong>Suggestion</strong>.</p>
<p>If you have used this plugin on your blog, post the address of your blog too, so that other can have a look.<br />
In case you need any assistance, feel free to use the comment section below</p>
<h3>Related Posts</h3>
<ol>
		<li><a href="http://webdeveloperplus.com/wordpress/how-to-ajaxify-wordpress-comment-posting/" rel="bookmark">
												How To: AJAXify WordPress Comment Posting</a><!-- (10.9)--></li>
		<li><a href="http://webdeveloperplus.com/design/10-ways-to-make-your-blogs-comment-form-stand-out/" rel="bookmark">
								<img src="http://thumbs.webdeveloperplus.com/img/2009/10/comment-form-tips-thumb.jpg" alt="10 Ways To Make Your Blog&#8217;s Comment Form Stand Out" width="40px" height="40px" />				10 Ways To Make Your Blog&#8217;s Comment Form Stand Out</a><!-- (5.9)--></li>
		<li><a href="http://webdeveloperplus.com/jquery/ajax-multiple-file-upload-form-using-jquery/" rel="bookmark">
												AJAX Multiple File Upload Form Using jQuery</a><!-- (5.8)--></li>
	</ol>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>How To: AJAXify WordPress Comment Posting</title>
		<link>http://webdeveloperplus.com/wordpress/how-to-ajaxify-wordpress-comment-posting/</link>
		<comments>http://webdeveloperplus.com/wordpress/how-to-ajaxify-wordpress-comment-posting/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 12:51:13 +0000</pubDate>
		<dc:creator>Satbir</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[ajax comments]]></category>
		<category><![CDATA[wordpress hacks]]></category>
		<category><![CDATA[wordpress plugin]]></category>

		<guid isPermaLink="false">http://webdeveloperplus.com/?p=226</guid>
		<description><![CDATA[There are tons of WordPress Tutorials out there, but i haven&#8217;t found a single one that explains how to AJAX-ify WordPress comment posting on blog posts. Though there are some plugins for AJAX comments, but none seem to work with WordPress 2.8 or later versions. Today i will show you how to enable AJAX comments [...]]]></description>
			<content:encoded><![CDATA[<p>There are tons of WordPress Tutorials out there, but i haven&#8217;t found a single one that explains how to AJAX-ify WordPress comment posting on blog posts. Though there are some plugins for AJAX comments, but none seem to work with WordPress 2.8 or later versions. </p>
<p>Today i will show you how to enable AJAX comments with client side JavaScript comment form validation on your WordPress blog step by step that should work with most versions of WordPress. We&#8217;ll be modifying the WordPress theme files to do that. In few days, i&#8217;ll be releasing a WordPress plugin that will automatically enable AJAX Comments. So, do subscribe to our <a href="http://feeds2.feedburner.com/WebDeveloperPlus" title="Subscribe via RSS" >RSS Feed</a> or <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebDeveloperPlus&#038;loc=en_US" target="_blank" >e-mail updates</a> so that you are the first to know about it.</p>
<p>Let&#8217;s start with it.</p>
<h3>Functions.php file</h3>
<p>Add the following lines of code to your theme&#8217;s <em>functions.php</em> file. If your theme doesn&#8217;t have one, then create a new file and add it to your theme folder.</p>
<pre name="code" class="php" >
add_action('init', 'wdp_ajaxcomments_load_js', 10);
function wdp_ajaxcomments_load_js(){
		wp_enqueue_script('ajaxValidate', get_stylesheet_directory_uri().'/wdp-ajaxed-comments/js/jquery.validate.min.js', array('jquery'), '1.5.5');
		wp_enqueue_script('ajaxcomments', get_stylesheet_directory_uri().'/wdp-ajaxed-comments/js/ajax-comments.js',	array('jquery', 'ajaxValidate'), '1.1');
}
add_action('comment_post', 'wdp_ajaxcomments_stop_for_ajax',20, 2);
function wdp_ajaxcomments_stop_for_ajax($comment_ID, $comment_status){
	if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &amp;&amp; strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
	//If AJAX Request Then
		switch($comment_status){
			case '0':
				//notify moderator of unapproved comment
				wp_notify_moderator($comment_ID);
			case '1': //Approved comment
				echo &quot;success&quot;;
				$commentdata=&amp;get_comment($comment_ID, ARRAY_A);
				$post=&amp;get_post($commentdata['comment_post_ID']); //Notify post author of comment
				if ( get_option('comments_notify') &amp;&amp; $commentdata['comment_approved'] &amp;&amp; $post-&gt;post_author != $commentdata['user_ID'] )
					wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
				break;
			default:
				echo &quot;error&quot;;
		}
		exit;
	}
}
</pre>
<p><strong>Code Explanation</strong>: Firstly, we load required JavaScript files jQuery, jQuery Validation and ajax-comments.js using the <code>wp_enqueue_script()</code> function. <code>wp_enqueue_script()</code> accepts four parameters.</p>
<ul>
<li>First is the name of the handler for JavaScript file, you can use any name here for your own custom JavaScript, but for files that wordpress include by default like jQuery, there are pre-fixed handlers like &#8216;jquery&#8217;.</li>
<li>Second parameter is path to JavaScript file.</li>
<li>Third parameter is the array of name of handlers of JavaScript files, which must be loaded before the specified file.</li>
<li>Last is the version number of JavaScript file which you should use in case you use caching on your blog.</li>
</ul>
<p>The function <code>wdp_ajaxcomments_stop_for_ajax()</code> hooks onto the comment_post action which is triggered after a comment has been successfully added to the database. normally, what happens when you post a comment is that you are redirected back to the original post, but since we will be using AJAX, we don&#8217;t want a redirect but a message about the status of comment posted.</p>
<p>I recently read a nice post from David Walsh where he told about <a href="http://davidwalsh.name/detect-ajax" target="_blank">how to identify AJAX requests</a> using <code>$_SERVER["HTTP_X_REQUESTED_WITH"]</code> which is populated by most JavaScript frameworks like in our case jQuery.<br />
I used it to hook into <code>comment_post</code> and check whether it is AJAX request or normal request. If it is AJAX request then check the status of comment, if it is unapproved or approved, a message <em>&#8216;success&#8217;</em> is output and moderator or post author is notified accordingly.<br />
After identifying AJAX request, i stopped the script execution using <code>exit</code> statement so as to prevent the redirect.<br />
Now rest is the job of JavaScript code.</p>
<h3>The JavaScript Code</h3>
<p>You will need to add two JavaScript files <em>jquery.validate.min.js</em> and <em>ajax-comments.js</em> in the <em>js</em> directory of your theme(Files are available at the end of article). Create the <em>js</em> directory and add files if it does not exist. Though jQuery is required to work, but since it is included in WordPress by default, it will automatically be loaded as we specified it as a requirement while loading JavaScript files in <em>functions.php</em>.</p>
<p><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">jQuery Validation plugin</a> is used to validate comment form data.<br />
Here&#8217;s the code and explanation for <em>ajax-comments.js</em>.</p>
<pre name="code" class="js" >
jQuery('document').ready(function($){
	var commentform=$('form[action$=wp-comments-post.php]');
	commentform.prepend('&lt;div id=&quot;wdpajax-info&quot; &gt;&lt;/div&gt;');
	var infodiv=$('#wdpajax-info');
	commentform.validate({
		submitHandler: function(form){
			//serialize and store form data in a variable
			var formdata=commentform.serialize();
			//Add a status message
			infodiv.html('&lt;p&gt;Processing...&lt;/p&gt;');
			//Extract action URL from commentform
			var formurl=commentform.attr('action');
			//Post Form with data
			$.ajax({
				type: 'post',
				url: formurl,
				data: formdata,
				error: function(XMLHttpRequest, textStatus, errorThrown){
					infodiv.html('&lt;p class=&quot;wdpajax-error&quot; &gt;You might have left one of the fields blank.&lt;/p&gt;');
				},
				success: function(data, textStatus){
					if(data==&quot;success&quot;)
						infodiv.html('&lt;p class=&quot;wdpajax-success&quot; &gt;Thanks for your comment. We appreciate your response.&lt;/p&gt;');
					else
						infodiv.html('&lt;p class=&quot;wdpajax-error&quot; &gt;Error in processing your form.&lt;/p&gt;');
					commentform.find('textarea[name=comment]').val('');
				}
			});
		}
	});
});
</pre>
<p><strong>Code Explanation:</strong> Firstly, we append a div to comment form that will show AJAX status messages. Then we intialize jQuery Validation on comment form and if validation is successful, we will use jQuery AJAX to send the form data to the action URL of comment form and display a message accordingly.</p>
<p>Now, uptill here, if you have followed along and worked as told, you&#8217;ll find that AJAX comment posting works but validation doesn&#8217;t. Since, we are using jQuery Validation plugin, you will have to now edit the theme&#8217;s comments.php file.</p>
<h3>Enable Comment Form Validation</h3>
<p>Open your theme&#8217;s comments.php and add some CSS classes to comment form input fields as described:</p>
<ul>
<li>To comment author name input, add <code>class=&quot;required&quot;</code></li>
<li>To comment author email input, add <code>class=&quot;required email&quot;</code></li>
<li>To comment author URL input, add <code>class=&quot;url&quot;</code></li>
<li>To the comment textarea, add <code>class=&quot;required&quot;</code></li>
</ul>
<p><img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/ajax-comments-wordpress.png" alt="Modify comments.php file" title="Modify comments.php file" width="635" height="202" class="largepostimage" /></p>
<p>Now the comment form validation should work. But it might be having awkward looks or styles, now the last step is to customize the looks of messages using CSS.</p>
<h3>Customize the Styles of Messaages</h3>
<p>The error messages from validation use labels with CSS class <em>error</em>. While the AJAX post request status messages use two CSS classes, wdpajax-success for AJAX success and wdpajax-error for error message. Define the styles for these classes in your theme&#8217;s stylesheet file. Here&#8217;s what i have used:</p>
<pre name="code" class="css" >
.wdpajax-error{
	border:1px solid #f9d9c9;
	padding:5px;
	color:#ff3311;
}
.wdpajax-success{
	border:1px solid #339933;
	padding:5px;
	color:#339933;
}
label.error{
	float:none !important;
	padding-left:5px;
	color:#ff3311;
}
</pre>
<p>There you are with cool AJAX comments on your WordPress Blog.<br />
<img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/wdp-ajax-comments-wordpress.png" alt="AJAx WordPress Comments" title="AJAx WordPress Comments" width="520" height="379" class="largepostimage" /></p>
<p><a href="http://demo.webdeveloperplus.com/wordpress/" target="_blank" ><strong>Click here to view the Live demo</strong></a>(Try commenting on one of the posts).</p>
<p>To download the required files, <a href="http://demo.webdeveloperplus.com/source-code/wdp-ajaxcomments-theme-files.zip" target="_blank" ><strong>Click here</strong></a><br />
If you find any problems or error, do let me know in comments.</p>
<h3>Plugin</h3>
<p>If you find modifying theme file difficult, or if you regularly change your blog&#8217;s theme then it will be difficult for you to modify theme files every-time you apply a new theme. so, i have created a plugin named <a href="http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/"><strong>WDP AJAX Comments</strong></a> that will automatically add AJAX commenting to your blog.<br />
So, do subscribe to our <a href="http://feeds2.feedburner.com/WebDeveloperPlus" title="Subscribe via RSS" >RSS Feed</a> or <a href="http://feedburner.google.com/fb/a/mailverify?uri=WebDeveloperPlus&#038;loc=en_US" target="_blank" >email updates</a> so that you are among the first one to get the plugin.<br />
Enjoy!</p>
<h3>Related Posts</h3>
<ol>
		<li><a href="http://webdeveloperplus.com/wordpress/new-wordpress-plugin-wdp-ajax-comments/" rel="bookmark">
												New WordPress Plugin: WDP AJAX Comments</a><!-- (12.1)--></li>
		<li><a href="http://webdeveloperplus.com/design/10-ways-to-make-your-blogs-comment-form-stand-out/" rel="bookmark">
								<img src="http://thumbs.webdeveloperplus.com/img/2009/10/comment-form-tips-thumb.jpg" alt="10 Ways To Make Your Blog&#8217;s Comment Form Stand Out" width="40px" height="40px" />				10 Ways To Make Your Blog&#8217;s Comment Form Stand Out</a><!-- (8.1)--></li>
		<li><a href="http://webdeveloperplus.com/wordpress/12-lesser-known-but-useful-wordpress-hacks/" rel="bookmark">
								<img src="http://thumbs.webdeveloperplus.com/img/2009/10/wordpress-hacks-thumb.jpg" alt="12 Lesser Known But Useful WordPress Hacks" width="40px" height="40px" />				12 Lesser Known But Useful WordPress Hacks</a><!-- (6)--></li>
	</ol>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloperplus.com/wordpress/how-to-ajaxify-wordpress-comment-posting/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Create a Dynamic Scrolling Content Box Using AJAX</title>
		<link>http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/</link>
		<comments>http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 07:17:48 +0000</pubDate>
		<dc:creator>Satbir</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[user interface]]></category>

		<guid isPermaLink="false">http://webdeveloperplus.com/?p=175</guid>
		<description><![CDATA[Create a dynamic content box that loads more content as user scrolls down the content box just like Google reader fetches new items as you scroll down. This tutorial uses AJAX with jQuery to fetch new items for the dynamic content box as user scrolls down]]></description>
			<content:encoded><![CDATA[<p>If you have used <a href="http://www.google.com/reader/" target="_blank">Google Reader</a>, then you might have noticed the way Google Reader shows feed items, it loads up few items first when you click on a feed and as you scroll down to view more items, it fetches more items dynamically and adds it to the list.<br />
<img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/Google-Reader-loading-more.png" alt="Google Reader loading more items" title="Google Reader loading more items" width="308" height="167" class="largepostimage" /></p>
<p><a href="http://www.dzone.com" target="_blank">Dzone</a> also implements this technique to show items as you scroll down the list.<br />
<img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/dzone-loading-more-items.png" alt="Dzone dynamic scrolling list" title="Dzone dynamic scrolling list" width="290" height="144" class="largepostimage" /></p>
<p>Similarly, Facebook has also started using this technique to load more posts in user&#8217;s home page as you scroll down.<br />
Today, I am going to create a similar dynamic content box that loads more content dynamically as user scrolls to the bottom of the box.</p>
<p><a href="http://demo.webdeveloperplus.com/dynamic-scrollbox/" title="View the Demo(opens in new window)" target="_blank" ><strong>View Working Demo</strong></a></p>
<h3>The HTML Structure</h3>
<pre name="code" class="html" >
&lt;div id=&quot;container&quot;&gt;
	&lt;div id=&quot;scrollbox&quot; &gt;
		&lt;div id=&quot;content&quot; &gt;
			&lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt;
			&lt;p&gt;Ipsum lorem dolor amet sit&lt;/p&gt;
			&lt;p&gt;Dolor lorem ipsum amet tis&lt;/p&gt;
			&lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt;
			&lt;p&gt;Ipsum lorem dolor amet sit&lt;/p&gt;
			&lt;p&gt;Dolor lorem ipsum amet tis&lt;/p&gt;
			&lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt;
			&lt;p&gt;Ipsum lorem dolor amet sit&lt;/p&gt;
			&lt;p&gt;Dolor lorem ipsum amet tis&lt;/p&gt;
		&lt;/div&gt;
	&lt;/div&gt;
	&lt;p&gt;&lt;span id=&quot;status&quot; &gt;&lt;/span&gt;&lt;/p&gt;
&lt;/div&gt;
</pre>
<p>The <code>scrollbox div</code> is the one that has fixed width and height and houses the <code>content div</code> that stores the content. The <code>status span</code> is used to display number of loaded items or the status message of loading more items.</p>
<h3>The CSS Styles</h3>
<p>The height and width of scrolling div is fixed using CSS and its <code>overflow</code> property is set to auto so that it gets a scrollbar when content exceeds the height or width. But to prevent horizontal scrolling, i added <code>overflow-x:hidden </code>attribute so that scrolling only occurs up or down.</p>
<pre name="code" class="css" >
#container{
	width:400px;
	margin:0px auto;
	padding:40px 0;
}
#scrollbox{
	width:400px;
	height:300px;
	overflow:auto; overflow-x:hidden;
}
#container &gt; p{
	background:#eee;
	color:#666;
	font-family:Arial, sans-serif; font-size:0.75em;
	padding:5px; margin:0;
	text-align:right;
}
</pre>
<h3>The Concept</h3>
<p>Now to load more content on scroll, you will need to understand the concept behind the JavaScript code given below. To start with, i will be using, three attributes of DOM objects namely, <code>scrollHeight, clientHeight and scrollTop</code>. <code>scrollHeight</code> is the actual height of the inner content of a box, i.e. the actual height of content in the <code>scrollbox</code> container in our case. <code>clientHeight</code> specifies the height of the viewport container i.e. height of the visible region which is the height of <code>scrollbox div</code> in our case.<br />
The scrollTop is the distance in pixels the content which has moved up.</p>
<p>Here is an illustraton specifying these properties.<br />
<img src="http://webdeveloperplus.com/wp-content/uploads/2009/08/dynamic-content-on-scroll1.png" alt="Dynamic Scrolling Content Box" title="Dynamic Scrolling Content Box" width="632" height="500" class="largepostimage" /></p>
<p>When the <code>scrollTop</code> becomes equal to <code>scrollHeight-clientHeight</code>, then the scroll bar has reached its bottom position, so this is the point when we need to fetch new items. To make the scrolling box more useful, i added another value called <code>scrolloffset</code>, which keeps track of the fact that how many pixels before the bottom the scroll bar is, so that we can start fetching new items instead of the user scrolling to the bottom and then waiting for content to load.</p>
<h3>The JavaScript Code</h3>
<p>Here&#8217;s the JavaScript code you will need. As always, you will need to include the latest jQuery library into your page header before this code.</p>
<pre name="code" class="js" >
$('document').ready(function(){
	updatestatus();
	scrollalert();
});
function updatestatus(){
	//Show number of loaded items
	var totalItems=$('#content p').length;
	$('#status').text('Loaded '+totalItems+' Items');
}
function scrollalert(){
	var scrolltop=$('#scrollbox').attr('scrollTop');
	var scrollheight=$('#scrollbox').attr('scrollHeight');
	var windowheight=$('#scrollbox').attr('clientHeight');
	var scrolloffset=20;
	if(scrolltop&gt;=(scrollheight-(windowheight+scrolloffset)))
	{
		//fetch new items
		$('#status').text('Loading more items...');
		$.get('new-items.html', '', function(newitems){
			$('#content').append(newitems);
			updatestatus();
		});
	}
	setTimeout('scrollalert();', 1500);
}
</pre>
<p><strong>Code Explanation</strong>: The <code>updatestatus()</code> function counts the number of items loaded into the content and displays it in the status span. The <code>scrollalert</code> function checks the current scrollTop position of the <code>scrollbox div</code> and if the scroll bar is near the bottom area, new items are fetched using AJAX.<br />
Here i am using <em>new-items.html</em> that simply displays some items for the content box as this is just a demo. In actual application, you might employ some server side script that will load next items from the result set.</p>
<p>The content obtained via AJAX is then appended to the <code>content div</code> and <code>updatestatus()</code> is called to update the status message. <code>setTimeout</code> is used so that position of the scroll bar is checked periodically and if it matches condition, then new items should be fetched.</p>
<p>So, there you are with a dynamic content box just like Google Reader or DZone. </p>
<p><a href="http://demo.webdeveloperplus.com/dynamic-scrollbox/" title="View Demo" ><strong>View Working Demo</strong></a> or <a href="http://demo.webdeveloperplus.com/source-code/dynamic-scrollbox.zip" title="Download Source Code n Try" ><strong>Download Source Code n Try</strong></a><br />
Enjoy!</p>
<!--No related posts.-->
]]></content:encoded>
			<wfw:commentRss>http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>AJAX Multiple File Upload Form Using jQuery</title>
		<link>http://webdeveloperplus.com/jquery/ajax-multiple-file-upload-form-using-jquery/</link>
		<comments>http://webdeveloperplus.com/jquery/ajax-multiple-file-upload-form-using-jquery/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:12:39 +0000</pubDate>
		<dc:creator>Satbir</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jquery forms]]></category>
		<category><![CDATA[jquery plugins]]></category>
		<category><![CDATA[user interface]]></category>

		<guid isPermaLink="false">http://webdeveloperplus.com/?p=99</guid>
		<description><![CDATA[Multiple file upload forms are sometimes essential for your web application but managing upload from multiple file input boxes becomes a bit tedious and lengthy. But thanks to jQuery using which we can make this task easier too. Today I'm going to create an AJAXified multiple file upload form that uses a lot lesser server side code and also provides a very nice user interface.]]></description>
			<content:encoded><![CDATA[<p><img src="http://webdeveloperplus.com/wp-content/uploads/2009/06/AJAX-Multi-File-Upload-jQuery.jpg" alt="AJAX Multiple File Upload using jQuery" title="AJAX Multiple File Upload using jQuery" width="521" height="379" class="alignnone size-full wp-image-100" /></p>
<p>Multiple file upload forms are sometimes essential for your web application but managing upload from multiple file input boxes becomes a bit tedious and lengthy. But thanks to jQuery using which we can make this task easier too. Today I&#8217;m going to create an AJAXified multiple file upload form that uses a lot lesser server side code and also provides a very nice user interface.</p>
<p>For this tutorial, i&#8217;ll be using latest version of jQuery and  <a href="http://valums.com/ajax-upload/" target="_blank">Ajax upload</a> library by <a href="http://valums.com" target="_blank" >Andrew Valums</a>. Grab both libraries and add them to your page header</p>
<h3>The HTML Structure</h3>
<p>As i said, the upload form will be intuitive, we won&#8217;t be using the classic File input box. So, First of all create the upload button on which user will click to open the File Selection dialog.</p>
<pre name="code" class="html" >
&lt;div id=&quot;upload&quot; &gt;Upload File&lt;/div&gt;
</pre>
<p>You can use any element you want for the button. I just applied some basic style to this div, you can apply any style you wish to make it look more attractive.</p>
<pre name="code" class="css" >
#upload{
	margin:30px 200px; padding:15px;
	font-weight:bold; font-size:1.3em;
	font-family:Arial, Helvetica, sans-serif;
	text-align:center;
	background:#f2f2f2;
	color:#3366cc;
	border:1px solid #ccc;
	width:150px;
	cursor:pointer !important;
	-moz-border-radius:5px; -webkit-border-radius:5px;
}
</pre>
<p>I also added a span element next to upload button to show various messages during processing. And to show the uploaded files to the user, i added an unordered list next to the button. Here&#8217;s the complete html code.</p>
<pre name="code" class="html" >
&lt;!-- Upload Button--&gt;
&lt;div id=&quot;upload&quot; &gt;Upload File&lt;/div&gt;&lt;span id=&quot;status&quot; &gt;&lt;/span&gt;
&lt;!--List Files--&gt;
&lt;ul id=&quot;files&quot; &gt;&lt;/ul&gt;
</pre>
<h3>The PHP Code</h3>
<p>To upload the files to server, here&#8217;s a basic file uploading script in PHP that displays &#8216;success&#8217; if file uploaded successfully otherwise displays &#8216;error&#8217;.</p>
<p><em>upload-file.php</em></p>
<pre name="code" class="html" >
&lt;?php
$uploaddir = './uploads/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 

if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
  echo &quot;success&quot;;
} else {
	echo &quot;error&quot;;
}
?&gt;
</pre>
<h3>The JavaScript Code</h3>
<p>And lastly the javascript part. The AJAX Upload library you included earlier will do all the heavy lifting for you. Here&#8217;s the JavaScript Code you&#8217;ll need.</p>
<pre name="code" class="js" >
$(function(){
	var btnUpload=$('#upload');
	var status=$('#status');
	new AjaxUpload(btnUpload, {
		action: 'upload-file.php',
		//Name of the file input box
		name: 'uploadfile',
		onSubmit: function(file, ext){
			if (! (ext &amp;&amp; /^(jpg|png|jpeg|gif)$/.test(ext))){
                  // check for valid file extension
				status.text('Only JPG, PNG or GIF files are allowed');
				return false;
			}
			status.text('Uploading...');
		},
		onComplete: function(file, response){
			//On completion clear the status
			status.text('');
			//Add uploaded file to list
			if(response===&quot;success&quot;){
				$('&lt;li&gt;&lt;/li&gt;').appendTo('#files').html('&lt;img src=&quot;./uploads/'+file+'&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;'+file).addClass('success');
			} else{
				$('&lt;li&gt;&lt;/li&gt;').appendTo('#files').text(file).addClass('error');
			}
		}
	});
});
</pre>
<p><strong>Code explanation:</strong> to use the AJAX Upload library we need to initialize the AjaxUpload object and provide it with parameters. The first parameter is the id of the button element on which the user will click and second is the server side script that&#8217;ll handle file upload. The second parameter can accept an array of various options to give you more control over the process.</p>
<p>And that&#8217;s exactly what i&#8217;ve done. </p>
<ul>
<li>The <em>action</em> field is the path to server side script,</li>
<li> <em>name</em> is the name of file input box(hidden) which will be used for upload. If you change this value, make sure to change the server side script corresspondinly.</li>
<li><em>onSubmit</em> lets you perform some function before the file is uploaded e.g. you can check the file extension like i&#8217;ve done above or show a status message.</li>
<li><em>onComplete</em> lets you perform some action after the upload is complete e.g. I&#8217;ve shown the uploaded image to the user.</li>
</ul>
<p>And if you want to limit the number of files that a user can upload at a time, simply use <em>this.disable()</em> within <em>onSubmit</em> or <em>onComplete</em> to disable the upload button after checking for some condition.</p>
<p><em>Note</em>: The file upload using AJAX is not true ajax as it uses hidden iframe to upload the form data but his whole process is transparent by using the AJAX Upload library and gives a feel of AJAXified file upload.</p>
<p><strong><a href="http://demo.webdeveloperplus.com/ajax-file-upload/" title="View Working Demo" >View Working Demo</a> or <a href="http://demo.webdeveloperplus.com/source-code/ajax-file-upload.zip" >Download Source Code n Try</a></strong>.</p>
<h3>Related Posts</h3>
<ol>
		<li><a href="http://webdeveloperplus.com/jquery/multiple-file-upload-with-progress-bar-using-jquery/" rel="bookmark">
								<img src="http://thumbs.webdeveloperplus.com/img/2009/10/jquery-upload-progress-bar-thumb.jpg" alt="Multiple File Upload With Progress Bar Using jQuery" width="40px" height="40px" />				Multiple File Upload With Progress Bar Using jQuery</a><!-- (17.6)--></li>
		<li><a href="http://webdeveloperplus.com/jquery/create-a-dynamic-scrolling-content-box-using-ajax/" rel="bookmark">
												Create a Dynamic Scrolling Content Box Using AJAX</a><!-- (7.2)--></li>
		<li><a href="http://webdeveloperplus.com/php/ajax-user-poll-using-jquery-and-php/" rel="bookmark">
								<img src="http://thumbs.webdeveloperplus.com/img/2009/10/user-poll-thumb.png" alt="AJAX User Poll Using jQuery and PHP" width="40px" height="40px" />				AJAX User Poll Using jQuery and PHP</a><!-- (6.4)--></li>
	</ol>
]]></content:encoded>
			<wfw:commentRss>http://webdeveloperplus.com/jquery/ajax-multiple-file-upload-form-using-jquery/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
	</channel>
</rss>

