<?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 Design Talk &#187; JQuery</title>
	<atom:link href="http://www.web-design-talk.co.uk/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.web-design-talk.co.uk</link>
	<description>Web Design &#38; Development Blog</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:50:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>JQuery Hover Effect Image Gallery For eCommerce</title>
		<link>http://www.web-design-talk.co.uk/200/jquery-hover-effect-image-gallery-for-ecommerce/</link>
		<comments>http://www.web-design-talk.co.uk/200/jquery-hover-effect-image-gallery-for-ecommerce/#comments</comments>
		<pubDate>Tue, 04 May 2010 16:58:01 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=200</guid>
		<description><![CDATA[After searching  for a simple jquery image gallery for an online store I decided to make my own. There were several solutions that were close to what I wanted, but quite there. The project required an simple large vierw of an image with multiple thumbnails below, when hovered upon switched the large image. The requirements [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" style="border: 1px solid #cccccc; padding: 2px 2px 2px 0px; margin-right: 15px;" title="web design talk jquery gallery" src="http://www.web-design-talk.co.uk/wp-content/uploads/2010/05/web-design-talk-jquery-gallery.png" alt="web design talk jquery gallery" width="210" height="255" />After searching  for a simple jquery image gallery for an online store I decided to make my own. There were several solutions that were close to what I wanted, but quite there. The project required an simple large vierw of an image with multiple thumbnails below, when hovered upon switched the large image. The requirements were as follows: cross browser compatible (IE6 plus, Chrome and Firefox &#8211; all the major browsers), have a nice fading effect between image changes and be totally degradable if JavaScript was turned off.</p>
<p>First of all, take a peek and the finished <a href="http://www.web-design-talk.co.uk/examples/5/">JQuery image gallery</a> &#8211; I&#8217;ve put the gallery in context on a product detail page for an ecommerce store.</p>
<h4>JavaScript and JQuery to the Rescue!</h4>
<p>JQuery was the <strong>JavaScript framework </strong>of choice and made it easy to get things up and running. Techniqually the same effect can be achieved without JQuery, but for simplicity I&#8217;m using JQuery. The code is quite easy to follow and I&#8217;ll just pickup  up on the main parts.</p>
<p>Firstly we&#8217;ll start off with the HTML &#8211; simply two dividers, with an unordered lists for the thumbnail images:</p>
<pre class="brush: xml;">
&lt;div id=&quot;bigpic&quot; class=&quot;b&quot;&gt;&lt;img src=&quot;images/big/iphone-3-big.jpg&quot; alt=&quot;iPod Shuffle 16GB Zoom View&quot; /&gt;
&lt;p id=&quot;desc&quot;&gt;iPod Shuffle 16GB Zoom View&lt;/p&gt;

&lt;/div&gt;

&lt;div id=&quot;thumbs&quot;&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a rel=&quot;images/small/iphone-1-small.jpg&quot; href=&quot;images/big/iphone-1-big.jpg&quot;&gt;
&lt;img src=&quot;images/small/iphone-1-small.jpg&quot; alt=&quot;iPod Shuffle Front View In Blue!&quot; /&gt;
&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt; &lt;a rel=&quot;images/small/iphone-2-small.jpg&quot; href=&quot;images/big/iphone-2-big.jpg&quot;&gt;
&lt;img src=&quot;images/small/iphone-2-small.jpg&quot; alt=&quot;iPod Shuffle Dual View Grey!&quot; /&gt;
&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt; &lt;a rel=&quot;images/small/iphone-3-small.jpg&quot; href=&quot;images/big/iphone-3-big.jpg&quot;&gt;
&lt;img src=&quot;images/small/iphone-3-small.jpg&quot; alt=&quot;iPod Shuffle 16GB Zoom View&quot; /&gt;
&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</pre>
<p>The main image is housed in the divider with the id of &#8216;bigpic&#8217;. I&#8217;ve also added a description below the big image &#8211; this will change when the image is hovered over to the thumbnail alternate text. Each thumbnail link goes directly to the enlarged version of the image &#8211; this way, the gallery will still work when JavaScript is turned off.</p>
<h4>The JQuery Magic &#8230;.</h4>
<p>Right after including the JQuery library (I recommend you make use of Google CDN for this &#8211; there are many <a href="http://www.web-design-talk.co.uk/81/reasons-to-let-google-host-your-jquery-files/">reasons to let Google host your JQuery files</a>) I have included an int,js file &#8211; this contains all the gallery JavaScript. The int file is a small file that catches a hover on each thumbnail and switches the main gallery image (Our &#8216;bigpic&#8217; divider). All the code is contained within the mahic document.ready listener. The first half of the file catches the hover on a thumbnail:</p>
<pre class="brush: jscript;">
$('#thumbs ul li a').hover(
		function() {
			var currentBigImage = $('#bigpic img').attr('src');
			var newBigImage = $(this).attr('href');
			var currentThumbSrc = $(this).attr('rel');
			switchImage(newBigImage, currentBigImage, currentThumbSrc);
		},
		function() {}
	);
</pre>
<p>Here we are getting the currentBig image href, the new big image (from our thumbs href) and current thumbnail src. The second empty fucntion is included to say &#8216;don&#8217;t do anything when hovering away&#8217;. Exclude this empty function and the hover event will continue to fire when you leave the image. Now we have these three variables we pass them to our switchImage function, code below:</p>
<pre class="brush: jscript;">
function switchImage(imageHref, currentBigImage, currentThumbSrc) {

		var theBigImage = $('#bigpic img');

		if (imageHref != currentBigImage) {

			theBigImage.fadeOut(250, function(){
				theBigImage.attr('src', imageHref).fadeIn(250);

				var newImageDesc = $(&quot;#thumbs ul li a img[src='&quot;+currentThumbSrc+&quot;']&quot;).attr('alt');
				$('p#desc').empty().html(newImageDesc);
			});

		}

	}
</pre>
<p>This is quite self explanatory. The first check made is that the current big image is not the same as the target big image &#8211; if this was true we would fade in and out the same image, which leads to a rather weird looking effect. If both paths match, the user has hovered over the thumbnail of the current big image (in this case nothing would happen). Hovering over any other image will result in the current big image fading out, setting the big image src to the src from the thumbnail (the new image) and populating our description paragraph.</p>
<h4>Using the Gallery in a Production Website</h4>
<p>In the example 3 static large and small images have been manually added for simplicity. In a real website, maybe an online store, you would retrieve this information from a database. Another thing to note is image sizes. I always find it useful to let users upload whatever file resolution they want, giving them the exact pixels as a recomendation. To ensure that a particularly large file wouldn&#8217;t break the layout I tend to sue image resizing scripts, such as the <a href="http://shiftingpixel.com/2008/03/03/smart-image-resizer/">excellent smart image resizer from shifting pixel</a> &#8211; uploaded images can be automatically scaled to dimensions of your choosing.</p>
<p>Also, at some point I&#8217;ll learn about making your owns plugins for JQuery and turn this into a self contained plugin <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>There&#8217;s not a lot more to this to be honest. tested in IE6, IE7, IE8, Chrome and Firefox &#8211; all working nicely.</p>
<p>Take a look at the <a href="http://www.web-design-talk.co.uk/examples/5/">final result</a> or <a href="http://www.web-design-talk.co.uk/wp-content/uploads/2010/05/jquery-gallery--www.web-design-talk.co.uk.zip">download the source files</a>. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/200/jquery-hover-effect-image-gallery-for-ecommerce/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Enhanced Visitor Event Tracking With Google Analytics and JQuery</title>
		<link>http://www.web-design-talk.co.uk/172/enhanced-visitor-event-tracking-with-google-analytics-and-jquery/</link>
		<comments>http://www.web-design-talk.co.uk/172/enhanced-visitor-event-tracking-with-google-analytics-and-jquery/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 19:59:44 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[SEO]]></category>
		<category><![CDATA[google analytics]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=172</guid>
		<description><![CDATA[Google Analytics has fast become the industry standard to track a plethora of web based information about your website. Whilst being totally free and easy to setup, you are limited to tracking elements that physically render in the browser &#8211; so items such as PDF, ZIP and RSS feeds links are not tracked, this because [...]]]></description>
			<content:encoded><![CDATA[<p>Google Analytics has fast become the industry standard to track a plethora of web based information about your website. Whilst being totally free and easy to setup, you are limited to tracking elements that physically render in the browser &#8211; so items such as PDF, ZIP and RSS feeds links are not tracked, this because Google Analytics has a great reliance upon JavaScript. However, tracking such links can be achieved with a small amount of extra work.</p>
<p>Personally, I wasn&#8217;t aware you could track specific links with Analytics and only ever considored this when a client asked &#8216;why doesn&#8217;t Google show me the numbers of times my marketing report (<em>read: a PDF file</em>) has been clicked?&#8217; &#8211; a totally valid request that I wanted to investigate.</p>
<h3 style="font-size:12px;">Use JQuery to improve Google Analytics and track downloads, RSS, Email &amp; external links</h3>
<p>First things first, make sure you have a google Analytics account, the latest version of JQuery and the latest version of the analytics code running on your website <img src='http://www.web-design-talk.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>As with the majority of the JQuery magic, everything happens within the doc ready event listener &#8211; this will used to capture various clicks to select elements.</p>
<p><strong>Tracking Download Link Clicks (PDF, ZIPs etc.)</strong></p>
<pre class="brush: jscript;">
$(document).ready(function() {

	$(&quot;a[rel=download]&quot;).click( function() {
		var fileName = $(this).attr(&quot;href&quot;);
		pageTracker._trackPageview(fileName);
		return true;
	});

});
</pre>
<p>Then on every link you wish to track, simply add the rel attribute to your non HTML files as follows:</p>
<pre class="brush: xml;">
&lt;a href=&quot;myData.zip&quot; rel=&quot;download&quot;&gt;Download My ZIP Data File&lt;/a&gt;
</pre>
<p><strong>Tracking Downloads of Specifc File Types (E.g. PDF files)</strong></p>
<p>Using the dollar sign to match against links that end in .pdf (or any extension you wish to track).</p>
<pre class="brush: jscript;">
$(document).ready(function() {

	$(&quot;a[href$=pdf]&quot;).click( function() {
		var myPDF = &quot;/pdfDownloads/&quot; . $(this).attr(&quot;href&quot;);
		pageTracker._trackPageview(myPDF);
		return true;
	});

});
</pre>
<p>The /pdfDownloads/ is used to identify and seperate report data within Google Analytics.</p>
<p><strong>Tracking the click of a specific link such as an RSS feed</strong></p>
<p>Simply add an identifier to your RSS feed link (in this example the link was given an id of &#8216;rssFeed&#8217;): </p>
<pre class="brush: jscript;">
$(document).ready(function() {

	$(&quot;a#rssFeed&quot;).click( function() {
		pageTracker._trackEvent(&quot;RSS&quot;, &quot;RSS Subscriber Link Clicked&quot;);
		return true;
	});

});
</pre>
<p><strong>Tracking mailto: Link Clicks</strong></p>
<pre class="brush: jscript;">
$(document).ready(function() {

	$(&quot;a[href^=mailto:]&quot;).click( function() {
		pageTracker._trackEvent(&quot;Mail&quot;, &quot;User clicked on mailto link&quot;);
		return true;
	});

});
</pre>
<p><strong>Tidying up&#8230;.</strong></p>
<p>You should also disable the clicked element to prevent multiple event recording and provide feedback. To do this, simple add the following at the start of each piece of code &#8211; disbabling the element and changing the cursor to an egg timer (although you could display a small graphic to make things look prettier): </p>
<pre class="brush: jscript;">
$(this).css(&quot;cursor&quot;, &quot;wait&quot;);
$(this).attr(&quot;disabled&quot;, true);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/172/enhanced-visitor-event-tracking-with-google-analytics-and-jquery/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Getting Multiple Array Form Values With PHP</title>
		<link>http://www.web-design-talk.co.uk/94/getting-multiple-array-form-values-with-php/</link>
		<comments>http://www.web-design-talk.co.uk/94/getting-multiple-array-form-values-with-php/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 21:38:46 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[add form inputs]]></category>
		<category><![CDATA[associative array]]></category>
		<category><![CDATA[jquery append]]></category>
		<category><![CDATA[multiple form values]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=94</guid>
		<description><![CDATA[Further to my article on using JQuery to dynamically append form elements, I have come across situations where multiple items should be appended to the form each time, as opposed to a single input in my article (I did this simplicity). For example, at work I&#8217;m currently working on an internal system whereby a user [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 130px"><img src="http://www.web-design-talk.co.uk/images/phparray.jpg" alt="php array code" width="120" height="111" /><p class="wp-caption-text">PHP Arrays</p></div>
<p>Further to my <a href="http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/">article on using JQuery to dynamically append form elements</a>, I have come across situations where multiple items should be appended to the form each time, as opposed to a single input in my article (I did this simplicity). For example, at work I&#8217;m currently working on an internal system whereby a user needs to add an unlimited amount of client contacts for a client. Pressing the &#8216;add contact&#8217; link will append 3 fields &#8211; one for conatct name, contact telephone and contact email. Each of these fields are named exactly the same way as before (using square brackets at the end of the name E.g. &#8216;name[]&#8216;) and appended the same way using JQuery.</p>
<p>There are lots of articles floating about explaing how to add fields, but I&#8217;ve not yet seen anything explaining how to retreive multiple elements like this.</p>
<p>The only differnce arises when retreiving these multiple values from the PHP&#8217;s POST array. In the example I have appended 3 inputs, named cname[], cemail[] and ctel[]. The values of each can be retreived using a slightly enchanced for loop:</p>
<pre class="brush: php;">
if (isset($_POST['cname'])) {
for ( $i=0;$i&lt;count($_POST['cname']);$i++) {
$contactname = $_POST['cname'][$i];
$conatctemail = $_POST['cemail'][$i];
$contacttel = $_POST['ctel'][$i];
}
}
</pre>
<p>That&#8217;s really all there is to it and I&#8217;m finding that the latter comes in useful quit regularly in every day projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/94/getting-multiple-array-form-values-with-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Reasons to let Google Host your JQuery Files</title>
		<link>http://www.web-design-talk.co.uk/81/reasons-to-let-google-host-your-jquery-files/</link>
		<comments>http://www.web-design-talk.co.uk/81/reasons-to-let-google-host-your-jquery-files/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 20:09:08 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[CDN]]></category>
		<category><![CDATA[Google]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=81</guid>
		<description><![CDATA[It&#8217;s often the case that I see busy sites hosting copies of the JQuery library locally. E.g &#60;script src=&#34;/js/jQuery.min.js&#34; type=&#34;text/javascript&#34;&#62;&#60;/script&#62; The preferred and better way is to host your JQuery through Google E.g. &#60;script src=&#34;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&#34; type=&#34;text/javascript&#34;&#62;&#60;/script&#62; So, why is this better? Well there are several valid reasons: CDN (Content Delivery Network) &#8211; Google&#8217;s datacenters are [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s often the case that I see busy sites hosting copies of the JQuery library locally. E.g</p>
<pre class="brush: jscript;">&lt;script src=&quot;/js/jQuery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre>
<p>The preferred and better way is to host your JQuery through Google E.g.</p>
<pre class="brush: jscript;">&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre>
<p>So, why is this better? Well there are several valid reasons:</p>
<p><strong>CDN (Content Delivery Network) &#8211; </strong>Google&#8217;s datacenters are located over a range of locations and when a user requests content the closest location is automatically chosen. This is better because it does not force users to download from a single server location (E.g your server) and the chances are Google will be able to serve content faster than your webhost. A similar theory is used for the popularweb based game called <a href="http://www.quakelive.com" target="_blank">quakelive</a>. Usually <a href="http://en.wikipedia.org/wiki/Content_delivery_network" target="_blank">CDN</a>&#8216;s are a service you pay for, but you&#8217;re getting this free through Google!</p>
<p><strong>Less server load &#8211; </strong>When all your website&#8217;s files are located on a single server, downloading them simulainiously increases server load and some users will recieve delays while files download. By having an external location for your JQuery library the latter is not an issue.</p>
<p><strong>Improved caching &#8211; </strong>This is the biggest benefit as users will not have to re-download content. Hosting JQuery on your own server will cause a first time visitor to download the whole file, even if they have several copies of the same file from other sites. Through Google&#8217;s CDN, re-requests for the same file will result in a response to cache the file for up to one year, as it understands that it is a repeat request for a duplicate file.</p>
<p><strong>Local Bandwidth savings &#8211; </strong>by letting Google host the file for you, you are in essence saving bandwidth. For personal sites this may not be an issue, but busy sites will notice significant bandwidth savings.</p>
<p>Google actually suggests using a .load() function to load the library (see below), but this not only interrupts JQuery&#8217;s killer feature (document.ready), but also causes an extra <a href="http://en.wikipedia.org/wiki/HTTP_Requests" target="_blank">HTTP request</a>. Personally I prfer the old fashioned script method, even though there are <a href="http://stackoverflow.com/questions/208869/what-are-advantages-of-using-google-loadjquery-vs-direct-inclusion-of-ho" target="_blank">several other valid reasons</a> to use the .load() method.</p>
<pre class="brush: jscript;">
&lt;script type=&quot;text/javascript&quot;
        src=&quot;http://www.google.com/jsapi&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
  google.load(&quot;jquery&quot;, &quot;1.3.2&quot;);
  google.setOnLoadCallback(function() {
  });
&lt;/script&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/81/reasons-to-let-google-host-your-jquery-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding Unlimited Form Fields With JQuery and Saving to a Database</title>
		<link>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/</link>
		<comments>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 20:36:49 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[associative array]]></category>
		<category><![CDATA[dynamic form fields]]></category>
		<category><![CDATA[pgp]]></category>
		<category><![CDATA[sprintf]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=58</guid>
		<description><![CDATA[In this article I'll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we'll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):]]></description>
			<content:encoded><![CDATA[<p>In this article I&#8217;ll discuss how to add an unlimited number of additional form elements to a form and then save to a database. The latter part is the key here as a variety of tutorials exist on adding form elements, but I have yet to see anywhere that actually explains how to manipulate these added form fields. For example, how to get values to store them in a MySQL datbase. In the example we&#8217;ll have a simple user signup form where the user can add multiple fields to describe their favourite websites.  The basic Form HTML is as follows (nothing amazing, just a simple html form):</p>
<pre class="brush: xml; highlight: [10,11,12];">
&lt;script src=&quot;js/jquery.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;h1&gt;New User Signup&lt;/h1&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot;&gt;

  &lt;label for=&quot;name&quot;&gt;Username:&lt;/label&gt;
  &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
  &lt;label for=&quot;name&quot;&gt;Password:&lt;/label&gt;
  &lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;

   &lt;div id=&quot;container&quot;&gt;
      &lt;a href=&quot;#&quot;&gt;&lt;span&gt;» Add your favourite links.....&lt;/span&gt;&lt;/a&gt;
   &lt;/div&gt;

   &lt;input id=&quot;go&quot; class=&quot;btn&quot; name=&quot;btnSubmit&quot; type=&quot;submit&quot; value=&quot;Signup&quot; /&gt;
&lt;/form&gt;
</pre>
<p>The only part that isn&#8217;t standard is highlighted above. This is simply the link users click to add additional form fields on the fly. To make that happen we&#8217;ll need some <a href="http://www.JQuery" target="_blank">JQuery</a>:</p>
<pre class="brush: jscript;">
var count = 0;
$(function(){
	$('p#add_field').click(function(){
		count += 1;
		$('#container').append('&lt;strong&gt;Link #' + count + '&lt;/strong&gt;'+ '&lt;input id=&quot;field_' + count + '&quot; name=&quot;fields[]' + '&quot; type=&quot;text&quot; /&gt;' );
	});
});</pre>
<p><span id="more-58"></span></p>
<p>Jquery makes it very easy to add for elements using the <a href="http://docs.jquery.com/Manipulation/append" target="_blank">append </a>method. As we need to assign a unique name to each additional input (as we are allowing the user to add an indefinite amount of inputs) I&#8217;ll use a variable to keep track of the number of inputs added. Whenever an input is added, the counter variable increments by one. Another thing to notice is how the fields are named using &#8216;txt[]&#8216;. This creates an array of field names. So now when we click the &#8216;add_field&#8217; link, a new input field is appended to the contianer div &#8211; <a href="http://www.web-design-talk.co.uk/examples/2/1/" target="_self">view example</a>.</p>
<p>The only other order of business is to save this information to a database. This would be easy if we had static field names for the whole form, but we don&#8217;t. We have the following:</p>
<pre class="brush: xml;">&lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;password&quot; name=&quot;password&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_1&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_2&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
&lt;input id=&quot;field_3&quot; name=&quot;fields[]&quot; type=&quot;text&quot; /&gt;
.
.
.
etc.</pre>
<p>To capture this dynamic fields we&#8217;ll need to use a bit of PHP to loop through our posted array and saving the data using a many-many relationships using the following database structure &#8211; allowing a user to be associated with an unlimited number of websites:</p>
<div class="wp-caption alignnone" style="width: 510px"><img title="Data Structure For Users And Sites" src="http://www.web-design-talk.co.uk/images/users_sites.jpg" alt="Many-Many Data Structure For Users And Sites" width="500" height="125" /><p class="wp-caption-text">Many-Many Data Structure For Users And Sites</p></div>
<p>The following PHP code will grab our static and dynamic form values and insert into the above data structure. We&#8217;ll be using a class for the database to minimise the amount of code written. Firstly we&#8217;ll check if the forma has actually been submitted, grab the static values (username and password) and insert these into our users table:</p>
<pre class="brush: php;">//If form was submitted
if (isset($_POST['btnSubmit'])) {

	//create instance of database class
	$db = new mysqldb();
	$db-&amp;gt;select_db();

	//Insert static values into users table
	$sql_user = sprintf(&quot;INSERT INTO users (Username, Password) VALUES ('%s','%s')&quot;,
						mysql_real_escape_string($_POST['name']),
						mysql_real_escape_string($_POST['password']) );
	$result_user = $db-&amp;gt;query($sql_user);</pre>
<p>PHP&#8217;s <a href="http://www.php.net/sprintf">sprintf</a> function was used to format the sql query. I also escape input to prevent <a href="http://en.wikipedia.org/wiki/SQL_injection">nasty things from happening</a>. Now we need to check if the user has actually added any favourite websites in order to prevent a php error if we try to find non-existant values, if they have added a field we&#8217;ll need the user id number previously inserted into the users table, as we&#8217;ll need to insert this elsewhere later on:</p>
<pre class="brush: php;">//Check if user has actually added additional fields to prevent a php error
	if ($_POST['fields']) {

		//get last inserted userid
		$inserted_user_id = $db-&amp;gt;last_insert_id();</pre>
<p>Next we loop through the added form field values. This is where naming the field as in our append function as &#8216;txt[]&#8216; comes in, as all fields we added are submiited as an array called txt. Using a for each loop we can assign values to all dynamically added fields and save them to database. The $key is simply the fields name (field_1) and the $key is our actual value entered for that field. Now we know this, we can easily generate the sql statements within the loop. Firstly we add the website into the websites table, get the id of the inserted website and insert this into the the link table, using the userid from before. To finish, we display a confirmation message and disconnect from the database:</p>
<pre class="brush: php;">//Loop through added fields
		foreach ( $_POST['fields'] as $key=&amp;gt;$value ) {

			//Insert into websites table
			$sql_website = sprintf(&quot;INSERT INTO websites (Website_URL) VALUES ('%s')&quot;,
						    	   mysql_real_escape_string($value) );
			$result_website = $db-&amp;gt;query($sql_website);
			$inserted_website_id = $db-&amp;gt;last_insert_id();

			//Insert into users_websites_link table
			$sql_users_website = sprintf(&quot;INSERT INTO users_websites_link (UserID, WebsiteID) VALUES ('%s','%s')&quot;,
						    	   mysql_real_escape_string($inserted_user_id),
								   mysql_real_escape_string($inserted_website_id) );
			$result_users_website = $db-&amp;gt;query($sql_users_website);

		}

	} else {

		//No additional fields added by user

	}
	echo &quot;
&lt;h1&gt;User Added, &lt;strong&gt;&quot; . count($_POST['fields']) . &quot;&lt;/strong&gt; website(s) added for this user!&lt;/h1&gt;
&quot;;

	//disconnect mysql connection
	$db-&amp;gt;kill();</pre>
<p>As a result, our database should be populated as follows (these are screenshots from phpmyadmin):</p>
<div class="wp-caption alignnone" style="width: 510px"><img class=" " title="Many to many relationship" src="http://www.web-design-talk.co.uk/images/users_websites_data.jpg" alt="All our data tables are populated and the correct website and user id are inserted into the link table" width="500" height="278" /><p class="wp-caption-text">All our data tables are populated and the correct website and user id are inserted into the link table</p></div>
<p>You can view the <a href="http://www.web-design-talk.co.uk/examples/2/index-source.php">source of php file here</a> or <a href="http://www.web-design-talk.co.uk/examples/2/adding-form-elements-with-jquery.zip">download the original source files</a> (you&#8217;ll need to open classes/db.class.php and populate the sql variables with your own database details). The MySQL source to create the tables is also included in the source files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Simple Ajax Content Loading With JQuery</title>
		<link>http://www.web-design-talk.co.uk/30/simple-ajax-content-loading-with-jquery/</link>
		<comments>http://www.web-design-talk.co.uk/30/simple-ajax-content-loading-with-jquery/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 21:54:33 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax content loading]]></category>
		<category><![CDATA[ajax load]]></category>
		<category><![CDATA[jquery .load()]]></category>

		<guid isPermaLink="false">http://www.web-design-talk.co.uk/?p=30</guid>
		<description><![CDATA[Occasionally it is useful to silently load content into an area on a webpage. For example, you may have a list of recent comments that you want to refresh every minute. Using a <a href="http://en.wikipedia.org/wiki/Meta_refresh">meta refresh</a> is one option, but this would cause the whole page to refresh, which could annoy the user. The solution is Ajax, where I'll reload the content silently without a single page refresh.]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft" style="width: 239px"><a href="http://www.jquery.com"><img title="JQuery Logo" src="http://www.web-design-talk.co.uk/images/jquery_logo.png" alt="JQuery.com Logo" width="229" height="74" /></a><p class="wp-caption-text">JQuery - JavaScript Framework</p></div>
<p>Occasionally it is useful to silently load content into an area on a webpage. For example, you may have a list of recent comments that you want to refresh every minute. Using a <a href="http://en.wikipedia.org/wiki/Meta_refresh">meta refresh</a> is one option, but this would cause the whole page to refresh, which could annoy the user. The solution is Ajax, where I&#8217;ll reload the content silently without a single page refresh. Even writing the simplist of Ajax functions is quite painful and requires a fair few <a href="http://www.ajaxlines.com/ajax/stuff/article/simple_ajax_javascript_function.php" target="_blank">lines of code</a> to get things done. To make things simpler we&#8217;ll use my favourite JavaScript Framework,  <a href="http://www.jquery.com" target="_blank">JQuery</a>.</p>
<p>The plan is to have dynamic content loaded via Ajax and refresh every x seconds. We&#8217;ll also have a loading image to show the user something is actually happening behind the scenes, as having nothing while the content is loading could make the user leave. The latter is especially important when querying large sets of data, where a delay is possible. You can get your own loading images from the <a href="http://www.ajaxload.info/" target="_blank">ajax loading image site</a>.Now we have a plan, we&#8217;ll get right into it.</p>
<p>First course of action is to setup our basic html page. It&#8217;s nothing amazing, simply a centered divider with a seperate divider for the loading graphic. Here&#8217;s the code we&#8217;ll be using (for simplicity I&#8217;ve used the style tag for the css, as opposed to having a seperate css file:</p>
<p><span id="more-30"></span></p>
<pre class="brush: css; html-script: true;">&lt;!--
	body, h1 {
		font-family: Verdana, Arial, Helvetica, sans-serif;
		font-size: 0.8em;
	}
	a {
		text-decoration: none;
		color: #333;
	}
	a:hover {
		color: #ff0000;
	}

	#ajax-content {
		height: auto;
		width: 400px;
		border: 5px solid #ccc;
		margin: 0 auto;
		padding: 10px;
	}
	#loading {
		width: 66px;
		margin: 0 auto;
	}
--&gt;
&lt;script src=&quot;jquery-1.2.3.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;div id=&quot;loading&quot;&gt;
   &lt;img src=&quot;loader.gif&quot; alt=&quot;content is loading&quot; width=&quot;100&quot; height=&quot;100&quot; /&gt;
&lt;/div&gt;
</pre>
<p>The JQuery library has also been included in the header &#8211; you can download JQuery from the <a href="http://jquery.com/">official site</a>. Our actual content will be loaded into the div called &#8216;ajax-content&#8217;. For the content I&#8217;ve simply used an external php file that echos an <a href="http://www.w3schools.com/html/html_lists.asp">un-ordered list</a> and the current server time (you can then see that the content has been refreshed as the time changes). This has been done for simplicity, but you could easily query a database for information. PHP&#8217;s <a href="http://www.php.net/sleep">sleep function</a> was used to create a delay when loading the content, this way you can see something is actually happening. In reality you would never want to use sleep, as it simply delays loading times. The contents of the PHP file are as follows:</p>
<pre class="brush: php;">Last Updated: &quot;.date('h:i:s A').&quot;&quot;;
echo &quot;
&lt;h1&gt;Big List of Software&lt;/h1&gt;
&quot;;
echo &quot;
&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;RmBarCode 1.00a&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;ServerMask 3.0.4&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;LinkDeny 1.1.0&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Drag Racer 1&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Method123 Educational 3.6&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Easy St. Tycoon 1.0&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;iColorPicker 6.18&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;USB Virus Scan 2.3&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;AWP Light FREE 2.0&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;ZipEnable 3.0.2&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;w3compiler 1.1.2&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Remote Reboot 2.0&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Pixeur 2.9.0.15&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Mailing Master 2.0&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://link.com&quot;&gt;Rewards Multiply 2.0&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&quot;;
?&amp;gt;</pre>
<p>Now enters the magic, the JQuery. The following code is placed in script tags in the document head, I&#8217;ll explain line by line how this works.</p>
<pre class="brush: jscript;">function fetchAjaxContent() {
	var numRand = Math.floor(Math.random()*101);
	$('#ajax-content').load('ajax.php?UID=' + numRand);
}</pre>
<p>For simplicity I&#8217;ll add a generic function that load&#8217;s the contents of our PHP file (ajax.php) via Ajax. This simply uses <a href="http://docs.jquery.com/Ajax/load">JQuery&#8217;s load function</a> to load the contents of ajax.php. The random number is placed in the querystring to stop Internet Explorer caching the results. The latter could also be done server side via adding the below to ajax.php:</p>
<pre class="brush:php">header("cache-control: no-cache");</pre>
<p>The following code is placed inside JQuery&#8217;s document ready function. Firstly we hide the divider that will contain the ajax content. using more of JQuery&#8217;s built in ajax function, I use the <a href="http://docs.jquery.com/Ajax/ajaxStart">ajaxStart </a> function (that is executed whenever an AJAX request begins and there is none already active). In this instance the loading image is simply displayed. The opposite function used is <a href="http://docs.jquery.com/Ajax/ajaxStop">ajaxStop </a> (executed whenever all AJAX requests have ended) to hide the loading image and show the loaded content. Finally, we&#8217;ll call our function from previously, &#8216;fetchAjaxContent()&#8217; from within the JavaScript setInterval function (calls a function at specified intervals in milliseconds, every 5 seconds is used in this example). The complete code for the document ready looks like this:</p>
<pre class="brush: jscript;">$(document).ready(function() {

        $('#ajax-content').hide();

	$(&quot;#loading&quot;).ajaxStart(function(){
		$('#ajax-content').slideUp();
                $(this).show();
	});

	$(&quot;#loading&quot;).ajaxStop(function(){
	         $(this).hide();
	         $(&quot;#ajax-content&quot;).slideDown();
	}); 

	setInterval(&quot;fetchAjaxContent()&quot;, 5000);

});</pre>
<p>That&#8217;s it basically. You can now <a href="http://www.web-design-talk.co.uk/examples/1/" target="_self">view a working example</a> or <a href="http://www.web-design-talk.co.uk/examples/1/ajax-content-loading.zip">download the source files</a>. Notice how after each refresh, 5 seconds has elapsed in the printed output.</p>
<p>Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.web-design-talk.co.uk/30/simple-ajax-content-loading-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
