<?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>Kevin Dubois &#187; php</title>
	<atom:link href="http://kevindubois.com/blog/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://kevindubois.com/blog</link>
	<description>A LAMP consultant in the greater Salt Lake City Area</description>
	<lastBuildDate>Mon, 24 Oct 2011 16:32:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Rotating images using jQuery Ajax</title>
		<link>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/</link>
		<comments>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 23:20:21 +0000</pubDate>
		<dc:creator>Kevin Dubois</dc:creator>
				<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[slideshow]]></category>

		<guid isPermaLink="false">http://kevindubois.com/blog/?p=13</guid>
		<description><![CDATA[Tweet There are many ways to rotate images on a website.  In my case, I needed a way to rotate all images from a particular folder, and I wanted it so that I just have to dump images in the &#8230; <a href="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fkevindubois.com%2Fblog%2F2009%2F03%2F03%2Frotating-images-jquery-ajax%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"  data-text="Rotating images using jQuery Ajax" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>There are many ways to rotate images on a website.  In my case, I needed a way to rotate all images from a particular folder, and I wanted it so that I just have to dump images in the folder in order for the slideshow to pick them up.  ** NOTE: if you want a more advanced setup with database driven slides visit my <a title="Database driven jquery rotating slideshow demo" href="http://kevindubois.com/blog/2009/07/29/database-driven-jquery-rotating-slide-show-with-text-navigation-and-links-using-the-cycle-plugin/" target="_blank">post about database driven slideshows</a></p>
<p>For this tutorial I&#039;m going to use jQuery&#039;s framework and crossSlide, a plugin for jQuery which will allow smooth transitions between the images.  If you don&#039;t have it already, you can get jQuery at <a title="www.jquery.com" href="http://www.jquery.com" target="_blank">www.jquery.com</a> and the crossSlide plugin at <a title="www.gruppo4.com/~tobia/cross-slide.shtml" href="http://www.gruppo4.com/~tobia/cross-slide.shtml" target="_blank">www.gruppo4.com/~tobia/cross-slide.shtml</a> .  You&#039;ll also have to create a folder with images.  You&#039;ll get the best result if all images are identical in size.</p>
<p>A working example can be found at <a title="duboistechnologies.com" href="http://www.duboistechnologies.com">duboistechnologies.com</a> .</p>
<p>Download the code <a title="rotate images zip" href="http://kevindubois.com/blog/attachments/rotate_images.zip" target="_self">here</a></p>
<p>Ok, first things first: we&#039;ll need a placeholder for the images.  We&#039;ll need to specify a fixed height for this placeholder too, otherwise the content below will be jumping up and down every time a new image is loaded:</p>
<blockquote><p>&lt;div id=&#034;imgcontainer&#034; style=&#034;margin:0; width:46em; height:20em;&#034;&gt;<br />
&lt;img src=&#034;img/ajax-loader.gif&#034; style=&#034;margin-top:20%&#034; alt=&#034;loading&#8230;&#034; id=&#034;mainImg&#034; /&gt;<br />
&lt;/div&gt;</p></blockquote>
<p>I put an image in the div that will show an icon while the images are loaded&#8230;</p>
<p>Next step is to create a php page with some code to get all images from the folder.   The getfiles function will get all files and return a string that will be formatted so it can be parsed as separate objects by the javascript eval() function&#8230; but that&#039;s for later.  We just echo this string so our ajax call will pick it up.  The only thing you&#039;ll need to customize is the &#039;imagefolder&#039;&#8230; change this to whatever the name or path is to your folder:</p>
<blockquote><p>&lt;?php</p>
<p>echo getfiles(&#039;imagefolder&#039;);</p>
<p>function getfiles($folder=&#034;, $extensions=&#039;.*&#039;)<br />
{</p>
<p>// path:<br />
$folder = trim($folder);<br />
$folder = ($folder == &#034;) ? &#039;./&#039; : $folder;</p>
<p>// validate folder<br />
if (!is_dir($folder)){ die(&#039;invalid folder given!&#039;); }</p>
<p>// create files array<br />
$files = array();</p>
<p>// open directory<br />
if ($dir = @opendir($folder)){</p>
<p>// get all files:<br />
while($file = readdir($dir)){</p>
<p>if (!preg_match(&#039;/^\.+$/&#039;, $file) and<br />
preg_match(&#039;/\.(&#039;.$extensions.&#039;)$/&#039;, $file)){</p>
<p>// feed the array:<br />
$files[] = $file;<br />
}<br />
}<br />
// close directory<br />
closedir($dir);<br />
}<br />
else {<br />
die(&#039;Could not open the folder &#034;&#039;.$folder.&#039;&#034;&#039;);<br />
}</p>
<p>if (count($files) == 0){<br />
die(&#039;No files where found <img src='http://kevindubois.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> &#039;);<br />
}<br />
// shuffle the array so we have a random sequence of images<br />
shuffle($files);</p>
<p>$return = &#034;;<br />
foreach($files as $file){<br />
// format the string so we can later parse it into objects in javascript<br />
$return .= &#034;{&#039;src&#039;:'{$folder}/{$file}&#039;},&#034;;<br />
}<br />
$return = &#034;[".substr($return, 0,-1)."]&#034;;</p>
<p>// return the string:<br />
return $return;</p>
<p>}</p>
<p>?&gt;</p></blockquote>
<p>What we need to do now is set up an ajax call that gets the string of images the above function found in my images folder.  We&#039;ll also have to include the jquery and crossSlide packages:</p>
<blockquote><p>&lt;script type=&#034;text/javascript&#034; src=&#034;js/jquery.js&#034;&gt;&lt;/script&gt;<br />
&lt;script type=&#034;text/javascript&#034; src=&#034;js/jquery.cross-slide.js&#034;&gt;&lt;/script&gt;</p>
<p>&lt;script type=&#034;text/javascript&#034;&gt;<br />
$(document).ready(function(){loadImage();})</p>
<p>function loadImage(){<br />
// getimage.php is the php file that gets my images from the images folder<br />
$.post(&#034;getimage.php&#034;, function(data){<br />
if(data.length &gt;0) {<br />
// evaluate the string to a group of objects<br />
myObject = eval(data);<br />
// create a slideshow with the image objects and drop them in the placeholder<br />
$(&#039;#imgcontainer&#039;).crossSlide({sleep:5,fade:2},myObject);<br />
}<br />
});</p>
<p>}<br />
&lt;/script&gt;</p></blockquote>
<p>That&#039;s all there is to it.  This is a great alternative to flash and is much less cpu intensive!</p>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="kevin.dubois@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Rotating images using jQuery Ajax" /><input type="hidden" name="currency_code" value="USD" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://kevindubois.com/blog/wp-content/plugins/buy-me-beer/icon_beer.gif" align="left" alt="" title="" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=kevin.dubois@gmail.com&amp;currency_code=USD&amp;amount=1&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Rotating+images+using+jQuery+Ajax" target="paypal">Was this post helpful? Buy me a drink :-)</a></p>]]></content:encoded>
			<wfw:commentRss>http://kevindubois.com/blog/2009/03/03/rotating-images-jquery-ajax/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Group TD (table data) without using rowspan when left joining in mySQL</title>
		<link>http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/</link>
		<comments>http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 15:43:59 +0000</pubDate>
		<dc:creator>Kevin Dubois</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[group]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[left join]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[td]]></category>
		<category><![CDATA[tr]]></category>

		<guid isPermaLink="false">http://kevindubois.com/blog/?p=6</guid>
		<description><![CDATA[Tweet When outputting data from a database using left joins you often get redundant data in your output.  Say you have a table with vendor information, and a second table containing the customers for each vendor.  If you want to &#8230; <a href="http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fkevindubois.com%2Fblog%2F2008%2F10%2F30%2Fgroup-td-table-data-without-using-rowspan-when-left-joining-in-mysql%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/"  data-text="Group TD (table data) without using rowspan when left joining in mySQL" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>When outputting data from a database using left joins you often get redundant data in your output.  Say you have a table with vendor information, and a second table containing the customers for each vendor.  If you want to display a table with the vendor names and their customers, you&#039;ll most likely want to use a left join.</p>
<p>The problem is, when you output your data you&#039;ll repeat the vendor name for each customer they have.  It makes the table look cluttered and hard to read.  Not to worry though, there is an easy solution!</p>
<p>All you have to do is create 2 classes:</p>
<blockquote><p><span style="color: #800080;">.noblanktd{color:inherit; border-top:1px solid #000; border-top:1px solid #000}<br />
.blanktd{color:white; }</span></p></blockquote>
<p>and here&#039;s the php code:</p>
<blockquote><p><span style="color: #800080;">function outputmystuff(){<br />
// get your data here eg<br />
$query = &#034;SELECT v.id, v.name as vendorname, c.name as customername FROM vendors as v LEFT JOIN customers as c ON v.id = c.vendor_id&#034;;<br />
$result = mysql_query($query);<br />
</span></p>
<p><span style="color: #800080;">$oldID = false;<br />
while($row = mysql_fetch_assoc($result)){<br />
$ID = $row['id'];<br />
if($oldID == $ID){<br />
$class = &#039;blanktd&#039;;<br />
} else{<br />
$class = &#034;noblanktd&#034;;<br />
}<br />
$returnval .= &lt;&lt;&lt;EOD<br />
&lt;tr&gt;<br />
&lt;td class=&#034;{$class}&#034;&gt;{$row['vendorname']}&lt;/td&gt;<br />
&lt;td class=&#034;noblanktd&#034;&gt;{$row['customername']}&lt;/td&gt;<br />
&lt;/tr&gt;<br />
EOD;<br />
$oldID = $ID;</span></p>
<p><span style="color: #800080;">}</span></p>
<p><span style="color: #800080;">return $returnval;</span></p>
<p><span style="color: #800080;">}</span></p></blockquote>
<p>and last but not least the html</p>
<blockquote><p><span style="color: #800080;">&lt;table class=&#034;repTable&#034;&gt;<br />
&lt;tr&gt;<br />
&lt;th&gt;Vendor Name&lt;/th&gt;<br />
&lt;th&gt;Customer Name&lt;/th&gt;<br />
&lt;/tr&gt;<br />
&lt;?=outputmystuff()?&gt;<br />
&lt;/table&gt;</span></p></blockquote>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="kevin.dubois@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Group TD (table data) without using rowspan when left joining in mySQL" /><input type="hidden" name="currency_code" value="USD" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://kevindubois.com/blog/wp-content/plugins/buy-me-beer/icon_beer.gif" align="left" alt="" title="" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=kevin.dubois@gmail.com&amp;currency_code=USD&amp;amount=1&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Group+TD+(table+data)+without+using+rowspan+when+left+joining+in+mySQL" target="paypal">Was this post helpful? Buy me a drink :-)</a></p>]]></content:encoded>
			<wfw:commentRss>http://kevindubois.com/blog/2008/10/30/group-td-table-data-without-using-rowspan-when-left-joining-in-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding additional text fields with Javascript</title>
		<link>http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/</link>
		<comments>http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 16:11:46 +0000</pubDate>
		<dc:creator>Kevin Dubois</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://kevindubois.com/blog/?p=3</guid>
		<description><![CDATA[Tweet ** UPDATE: This functionality is outdated. Look at a more recent post on how to add more field to a form field: &#039;Add More&#039; form fields with javascript / jquery ** One of my clients needed the ability for &#8230; <a href="http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="bottomcontainerBox" style="border:1px solid #808080;background-color:#F0F4F9;">
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fkevindubois.com%2Fblog%2F2008%2F10%2F16%2Fadding-additional-text-fields-with-javascript%2F&amp;layout=button_count&amp;show_faces=false&amp;width=85&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width=85px; height:21px;" allowTransparency="true"></iframe></div>
			<div style="float:left; width:80px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<g:plusone size="medium" href="http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/"></g:plusone>
			</div>
			<div style="float:left; width:95px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;">
			<a href="http://twitter.com/share" class="twitter-share-button" data-url="http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/"  data-text="Adding additional text fields with Javascript" data-count="horizontal">Tweet</a>
			</div><div style="float:left; width:105px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script type="in/share" data-url="http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/" data-counter="right"></script></div>			
			<div style="float:left; width:85px;padding-right:10px; margin:4px 4px 4px 4px;height:30px;"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1&amp;r=http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/"></script></div>			
			</div><div style="clear:both"></div><div style="padding-bottom:4px;"></div><p>** UPDATE: This functionality is outdated.  Look at a more recent post on how to add more field to a form field:<br />
<a href="http://kevindubois.com/blog/2011/10/24/add-more-form-fields-with-javascript-jquery/">&#039;Add More&#039; form fields with javascript / jquery</a> **</p>
<p>One of my clients needed the ability for users to refer their family and friends to the website by sending them an email.   The idea was to have 5 fixed fields, and the ability to add more fields on the fly before hitting submit.</p>
<p><strong>Step 1</strong> is obviously adding the static html code (with a tiny bit of php to avoid redundancy):</p>
<blockquote><p>&lt;form action=&#034;mysubmitpage.php&#034; method=&#034;post&#034;&gt;<br />
&lt;div id=&#039;fields&#039;&gt;<br />
&lt;? for($i=1; $i&lt;6; $i++){?&gt;<br />
&lt;div&gt;<br />
name: &lt;input type=&#034;text&#034; name=&#034;refname&lt;?=$i?&gt;&#034; /&gt;</p>
<p>email: &lt;input type=&#034;text&#034; name=&#034;refemail&lt;?=$i?&gt;&#034;  /&gt;</p>
<p>&lt;/div&gt;<br />
&lt;? }?&gt;</p>
<p>&lt;/div&gt;</p>
<p>&lt;div style=&#034;margin-top:5px;&#034;&gt;<br />
&lt;input type=&#034;submit&#034; value=&#034;Send Invitations&#034; name=&#034;send&#034; /&gt;<br />
&lt;/div&gt;</p>
<p>&lt;/form&gt;</p></blockquote>
<p><strong>Step 2</strong> involves adding an &#039;add more&#039; link, which calls the javascript function that adds the field.  I&#039;m passing the number of the next field into the function, so when the time comes to submit it to the server I can distinguish the fields.  I would have preferred to use IE&#039;s outerHTML but that&#039;s not supported in my preferred browser firefox, so to keep everything neat I get the existing fields and append the new field to the end of the string.   I also replace the &#034;add more&#034; link so next time it is clicked the number 7, 8, 9 etc is passed.</p>
<blockquote><p>&lt;script language=&#034;javascript&#034;&gt;<br />
function addrefField(i){<br />
var newfieldContent = &#034;&lt;div&gt;name: &lt;input type=\&#034;text\&#034; name=\&#034;refname&#034;+i+&#034;\&#034; /&gt; email: &lt;input type=\&#034;text\&#034; name=\&#034;refemail&#034;+i+&#034;\&#034; /&gt; &lt;/div&gt; \n&#034;;</p>
<p>var oldfieldContent = document.getElementById(&#039;fields&#039;).innerHTML;</p>
<p>el = document.getElementById(&#039;fields&#039;);<br />
el.innerHTML = oldfieldContent + newfieldContent;</p>
<p>x = i + 1;<br />
var addmoreContent = document.getElementById(&#039;addmorelink&#039;);<br />
addmoreContent.innerHTML = &#039;&lt;div&gt;&lt;a href=&#034;javascript:addrefField(&#039;+x+&#039;)&#034;&gt;add more&lt;/a&gt;&lt;/div&gt;&lt;input type=&#034;hidden&#034; name=&#034;numberfields&#034; value=&#034;&#039;+i+&#039;&#034; /&gt;&#039;;</p>
<p>}</p>
<p>&lt;/script&gt;</p>
<p>&lt;div id=&#039;addmorelink&#039;&gt;<br />
&lt;a href=&#034;javascript:addrefField(6)&#034;&gt;add more&lt;/a&gt;<br />
&lt;input type=&#034;hidden&#034; name=&#034;numberfields&#034; value=&#034;6&#034; /&gt;<br />
&lt;/div&gt;</p></blockquote>
<p><strong>Step 3 </strong>: The above code seems to work fine in IE, but in Firefox there is a problem: the value of the existing fields is reset to whatever the DOM originally served up to the user.  So if the user had started to fill out the fields and then clicked the &#039;add more&#039; link, all fields would have been emptied.  With every problem comes a solution ofcourse.  I googled my problem and stumbled upon <a title="this post" href="http://dev-answers.blogspot.com/2007/08/firefox-does-not-reflect-input-form.html" target="_blank">this post</a>.  By adding a function (updateDOM()) on the onBlur event for each field, we can update the DOM to reflect the already inserted values.  So here&#039;s the entire code for the form:</p>
<blockquote><p>&lt;script language=&#034;javascript&#034;&gt;<br />
function addrefField(i){<br />
var newfieldContent = &#034;&lt;div&gt;name: &lt;input type=\&#034;text\&#034; name=\&#034;refname&#034;+i+&#034;\&#034; onBlur=\&#034;updateDOM(this)\&#034; /&gt; email: &lt;input type=\&#034;text\&#034; name=\&#034;refemail&#034;+i+&#034;\&#034; onBlur=\&#034;updateDOM(this)\&#034; /&gt; &lt;/div&gt; \n&#034;;</p>
<p>var oldfieldContent = document.getElementById(&#039;fields&#039;).innerHTML;</p>
<p>el = document.getElementById(&#039;fields&#039;);<br />
el.innerHTML = oldfieldContent + newfieldContent;</p>
<p>x = i + 1;<br />
var addmoreContent = document.getElementById(&#039;addmorelink&#039;);<br />
addmoreContent.innerHTML = &#039;&lt;div&gt;&lt;a href=&#034;javascript:addrefField(&#039;+x+&#039;)&#034;&gt;add more&lt;/a&gt;&lt;/div&gt;&lt;input type=&#034;hidden&#034; name=&#034;numberfields&#034; value=&#034;&#039;+i+&#039;&#034; /&gt;&#039;;</p>
<p>}</p>
<p>function updateDOM(inputField) {<br />
// if the inputField ID string has been passed in, get the inputField object<br />
if (typeof inputField == &#034;string&#034;) {<br />
inputField = document.getElementById(inputField);<br />
}</p>
<p>if (inputField.type == &#034;select-one&#034;) {<br />
for (var i=0; i&lt;inputField.options.length; i++) {<br />
if (i == inputField.selectedIndex) {<br />
inputField.options[inputField.selectedIndex].setAttribute(&#034;selected&#034;,&#034;selected&#034;);<br />
}<br />
}<br />
} else if (inputField.type == &#034;text&#034;) {<br />
inputField.setAttribute(&#034;value&#034;,inputField.value);<br />
} else if (inputField.type == &#034;textarea&#034;) {<br />
inputField.setAttribute(&#034;value&#034;,inputField.value);<br />
} else if ((inputField.type == &#034;checkbox&#034;) || (inputField.type == &#034;radio&#034;)) {<br />
if (inputField.checked) {<br />
inputField.setAttribute(&#034;checked&#034;,&#034;checked&#034;);<br />
} else {<br />
inputField.removeAttribute(&#034;checked&#034;);<br />
}<br />
}<br />
}</p>
<p>&lt;/script&gt;</p>
<p>&lt;form action=&#034;index.php&#034; method=&#034;post&#034;&gt;<br />
&lt;div id=&#039;fields&#039;&gt;<br />
&lt;? for($i=1; $i&lt;6; $i++){?&gt;<br />
&lt;div&gt;<br />
name: &lt;input type=&#034;text&#034; name=&#034;refname&lt;?=$i?&gt;&#034; onBlur=&#034;updateDOM(this)&#034; /&gt; email:<br />
&lt;input type=&#034;text&#034; name=&#034;refemail&lt;?=$i?&gt;&#034; onBlur=&#034;updateDOM(this)&#034; /&gt; &lt;/div&gt;<br />
&lt;? }?&gt;</p>
<p>&lt;/div&gt;<br />
&lt;div id=&#039;addmorelink&#039;&gt;<br />
&lt;a href=&#034;javascript:addrefField(6)&#034;&gt;add more&lt;/a&gt;<br />
&lt;input type=&#034;hidden&#034; name=&#034;numberfields&#034; value=&#034;6&#034; /&gt;<br />
&lt;/div&gt;<br />
&lt;div style=&#034;margin-top:5px;&#034;&gt;   &lt;input type=&#034;submit&#034; value=&#034;Send Invitations&#034; name=&#034;send&#034; /&gt; &lt;/div&gt;</p>
<p>&lt;/form&gt;</p></blockquote>
<p><strong>Step 4</strong> : All that&#039;s left is processing the fields on the next page:</p>
<blockquote><p>&lt;?php</p>
<p>$loops = $_POST['numberfields'];<br />
for($i=1;$i&lt;=$loops;$i++){</p>
<p>$namepost = &#034;refname&#034; . $i;<br />
$name = $_POST[$namepost];<br />
$emailpost = &#034;refemail&#034; . $i;<br />
$email = $_POST[$emailpost];<br />
if($name &amp;&amp; $email){<br />
echo $name .&#039; &#039; .$email;<br />
echo &#039;&lt;br /&gt;&#039;;<br />
}</p>
<p>}</p>
<p>?&gt;</p></blockquote>
<p class="buymebeer"><form action="https://www.paypal.com/cgi-bin/webscr" target="paypal" method="post"><input type="hidden" name="cmd" value="_xclick" /><input type="hidden" name="business" value="kevin.dubois@gmail.com" /><input type="hidden" name="return" value="" /><input type="hidden" name="item_name" value="Buy Me a Beer for Adding additional text fields with Javascript" /><input type="hidden" name="currency_code" value="USD" /><input type="hidden" name="amount" value="1" /><input type="image" src="http://kevindubois.com/blog/wp-content/plugins/buy-me-beer/icon_beer.gif" align="left" alt="" title="" hspace="3" /></form><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&amp;business=kevin.dubois@gmail.com&amp;currency_code=USD&amp;amount=1&amp;return=&amp;item_name=Buy+Me+a+Beer+for+Adding+additional+text+fields+with+Javascript" target="paypal">Was this post helpful? Buy me a drink :-)</a></p>]]></content:encoded>
			<wfw:commentRss>http://kevindubois.com/blog/2008/10/16/adding-additional-text-fields-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

