<?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>Alex's  Rocket &#187; form elements</title>
	<atom:link href="http://klalex.com/tag/form-elements/feed/" rel="self" type="application/rss+xml" />
	<link>http://klalex.com</link>
	<description>Developing web applications, startups, technologies</description>
	<lastBuildDate>Tue, 21 Jun 2011 13:45:30 +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>Using arrays in form element names</title>
		<link>http://klalex.com/2009/07/using-arrays-in-form-element-names/</link>
		<comments>http://klalex.com/2009/07/using-arrays-in-form-element-names/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 13:08:35 +0000</pubDate>
		<dc:creator>klalex</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[form elements]]></category>

		<guid isPermaLink="false">http://klalex.com/?p=68</guid>
		<description><![CDATA[Sometimes there is variable number of similar elements on a form. For example, phone numbers. To iterate all filled values on the server it will be nice to use arrays. And form element naming allow it: &#60;form method=&#34;post&#34;&#62; &#60;input type=&#34;text&#34; name=&#34;phone[0]&#34; /&#62; &#60;input type=&#34;text&#34; name=&#34;phone[1]&#34; /&#62; &#8230; &#60;/form&#62; Using PHP it&#8217;s easy to iterate through [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes there is variable number of similar elements on a form. For example, phone numbers. To iterate all filled values on the server it will be nice to use arrays. And form element naming allow it:</p>
<div class="codesnip-container" >
<div class="html4strict codesnip" style="font-family:monospace;"><span class="sc2">&lt;<a href="http://december.com/html/4/element/form.html"><span class="kw2">form</span></a> <span class="kw3">method</span><span class="sy0">=</span><span class="st0">&quot;post&quot;</span>&gt;</span><br />
<span class="sc2">&lt;<a href="http://december.com/html/4/element/input.html"><span class="kw2">input</span></a> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text&quot;</span> <span class="kw3">name</span><span class="sy0">=</span><span class="st0">&quot;phone[0]&quot;</span> <span class="sy0">/</span>&gt;</span><br />
<span class="sc2">&lt;<a href="http://december.com/html/4/element/input.html"><span class="kw2">input</span></a> <span class="kw3">type</span><span class="sy0">=</span><span class="st0">&quot;text&quot;</span> <span class="kw3">name</span><span class="sy0">=</span><span class="st0">&quot;phone[1]&quot;</span> <span class="sy0">/</span>&gt;</span><br />
&#8230;<br />
<span class="sc2">&lt;<span class="sy0">/</span><a href="http://december.com/html/4/element/form.html"><span class="kw2">form</span></a>&gt;</span></div>
</div>
<p>Using PHP it&#8217;s easy to iterate through those values now:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$_POST</span><span class="br0">&#91;</span><span class="st_h">&#8216;phone&#8217;</span><span class="br0">&#93;</span> <span class="kw1">as</span> phone<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
<span class="co1">// do something with phone, save in the database</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>But how to access those values with JavaScript? In case we need to validate required fields, for example. It&#8217;s easy to suppose that document.forms[0].phone[0].value returns value. Actually no. In this case we need to use document.forms[0].elements array and access by key:</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw3">alert</span><span class="br0">&#40;</span>document.<span class="me1">forms</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">elements</span><span class="br0">&#91;</span><span class="st0">&quot;phone[0]&quot;</span><span class="br0">&#93;</span>.<span class="me1">value</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>Using arrays in form element naming allows us to control the variable number of form elements. For example, we can add link &#8220;Add another phone&#8221; and when user clicks on it add new &lt;input type=&#8221;text&#8221; name=&#8221;phone[2]&#8221; /&gt; field. And we do not need to change anything in PHP code because it will iterate all values that we have passed to the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://klalex.com/2009/07/using-arrays-in-form-element-names/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

