<?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>Adrogen Blog &#187; JavaScript</title>
	<atom:link href="http://www.adrogen.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adrogen.com/blog</link>
	<description>our thoughts online</description>
	<lastBuildDate>Mon, 12 Apr 2010 23:03:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Checkbox Arrays and JavaScript and YOU!</title>
		<link>http://www.adrogen.com/blog/php-checkbox-arrays-javascript/</link>
		<comments>http://www.adrogen.com/blog/php-checkbox-arrays-javascript/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 22:54:29 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.adrogen.com/blog/?p=41</guid>
		<description><![CDATA[Have you ever had to collect a series of values from check boxes, or any other input for that matter and put them in $_POST arrays to save precious precious lines?  Ala:





Great right?  Because when the form is executed all values of &#8220;this_one&#8221; that were checked are conveniently stored in the array:

$_POST['this_one']

But&#8230; wait [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever had to collect a series of values from check boxes, or any other input for that matter and put them in $_POST arrays to save precious precious lines?  Ala:</p>
<pre name="code" class="html">
<input type="checkbox" name="this_one[]" value="1" />
<input type="checkbox" name="this_one[]" value="2" />
<input type="checkbox" name="this_one[]" value="3" />
</pre>
<p>Great right?  Because when the form is executed all values of &#8220;this_one&#8221; that were checked are conveniently stored in the array:</p>
<pre name="code" class="php">
$_POST['this_one']
</pre>
<p>But&#8230; wait a sec&#8230; what if you have some validation going on with this form and you have to use a client side script such as JavaScript to check whether or not those boxes are checked.</p>
<p>With the above HTML, <strong>you can&#8217;t use:</strong></p>
<pre name="code" class="javascript">
var chks = document.form_name.this_one;
</pre>
<p><strong>OR</strong></p>
<pre name="code" class="javascript">
var chks = document.form_name.this_one[];
</pre>
<p><strong>So what&#8217;s a young man or woman to do?</strong></p>
<p>Dry those tears buttercup, the answer is simpler than you think!  Just change the way you&#8217;re accessing your checkboxes (or input elements).  This way you can use your fancy PHP Array <strong>AND</strong> do your JavaScript validation, ala:</p>
<p><strong>The Fix:</strong></p>
<pre name="code" class="javascript">
var chks = document.getElementsByName('this_one[]');
</pre>
<p>Now you can run a JavaScript loop:</p>
<pre name="code" class="javascript">
var chks = document.getElementsByName('this_one[]');
var the_checked = false;
for (var i = 0; i < chks.length; i++) {
	if (chks[i].checked) {
		the_checked = true;
		break;
	}
}
if (the_checked == true) {
	alert('Cannot Proceed!  At least one box is Checked!');
	return false;
}
</pre>
<p><strong>AND</strong> still get your nice PHP Array:</p>
<pre name="code" class="php">
print_r($_POST['this_one']);
</pre>
<p>And it validates!  The world... the sun moon and stars... they be happy now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adrogen.com/blog/php-checkbox-arrays-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
