<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Posting Unchecked Checkboxes in HTML Forms</title>
	<atom:link href="http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/?source=rss</link>
	<description>A bit of my personal life, mainly focused on my kids. A bit of code, mainly focused on Wordpress and PHP. Overall, bits of nothing in particular.</description>
	<lastBuildDate>Wed, 23 May 2012 13:43:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Ross</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-197524</link>
		<dc:creator>Ross</dc:creator>
		<pubDate>Wed, 11 Jan 2012 05:38:38 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-197524</guid>
		<description>Thanks for this!  I can confirm that your method (hidden fields) works for simple jquery ajax requests where I use:
                dataString = $(&quot;#form&quot;).serialize();
and
	        $.ajax({
	        type: &quot;POST&quot;,
	        url: &quot;save_settings.php&quot;,
	        data: dataString,
	        dataType: &quot;json&quot;,
                ...</description>
		<content:encoded><![CDATA[<p>Thanks for this!  I can confirm that your method (hidden fields) works for simple jquery ajax requests where I use:<br />
                dataString = $(&#8220;#form&#8221;).serialize();<br />
and<br />
	        $.ajax({<br />
	        type: &#8220;POST&#8221;,<br />
	        url: &#8220;save_settings.php&#8221;,<br />
	        data: dataString,<br />
	        dataType: &#8220;json&#8221;,<br />
                &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bejoy</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-146680</link>
		<dc:creator>Bejoy</dc:creator>
		<pubDate>Wed, 16 Mar 2011 13:55:29 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-146680</guid>
		<description>I did like this.


............
ans in java script before submit..

var elem = document.getElementById(&quot;i_chkbox&quot;)
if (elem.checked == false){
elem.checked = true;
elem.value = &quot;off&quot;
}
...............

at the server side i get &quot;off&quot; or &quot;on&quot;</description>
		<content:encoded><![CDATA[<p>I did like this.</p>
<p>&#8230;&#8230;&#8230;&#8230;<br />
ans in java script before submit..</p>
<p>var elem = document.getElementById(&#8220;i_chkbox&#8221;)<br />
if (elem.checked == false){<br />
elem.checked = true;<br />
elem.value = &#8220;off&#8221;<br />
}<br />
&#8230;&#8230;&#8230;&#8230;&#8230;</p>
<p>at the server side i get &#8220;off&#8221; or &#8220;on&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-139094</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Thu, 20 Jan 2011 04:00:44 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-139094</guid>
		<description>Although I agree it is annoying that checkboxes and radio buttons do not get posted with &quot;empty&quot; values, many people don&#039;t seem to get it that if the box is not checked it doesn&#039;t have ANY value.  A significant number of developers use these boxes relying only on the name, but if there is no value then there is nothing to post, and if the box is not checked there is no value.  I have been coding these for years and one approach has been to include a hidden dummy (at the END of the form) with the same name and  checked.  This guarantees that the name will always be posted and always as an array, but also assumes the application is looking for specific values or ignoring empty values (a checkbox/radio button with no value= still has no real value). 

A second alternative is to send a hidden field containing a list of checkbox names, then at the host pre-process the input to interrogate this hidden list of checkbox names and add any with an appropriate &quot;missing&quot; indicator to the session environment, this assumes that variables can be added transparently at the host to the POSTed stream.

A third alternative is to use javascript on the client to use the hidden field mentioned in alternative #2 above when the form is submitted to interrogate the form fields and if none of the boxes are checked then create a dummy entry and attach it to the DOM to be sent (still assumes that the application wants a real value=).

The menu form shown earlier is a classic example of what should be a checkbox array where each value= is the name e.g.
type=&quot;checkbox&quot; name=&quot;sauces&quot; value=&quot;Apricot Sauce&quot;
type=&quot;checkbox&quot; name=&quot;sauces&quot; value=&quot;Marinara Sauce&quot;
etc.
so the application should be iterating through the array and checking the presence of the array before starting the iteration.

There are many pros and cons to the W3C &quot;successful&quot; behaviour of an individual HTML element in a POST or GET, but the fact remains we should be writing defensive code, after all we never know when a &#039;bot may be trying to infiltrate our site.

Phil</description>
		<content:encoded><![CDATA[<p>Although I agree it is annoying that checkboxes and radio buttons do not get posted with &#8220;empty&#8221; values, many people don&#8217;t seem to get it that if the box is not checked it doesn&#8217;t have ANY value.  A significant number of developers use these boxes relying only on the name, but if there is no value then there is nothing to post, and if the box is not checked there is no value.  I have been coding these for years and one approach has been to include a hidden dummy (at the END of the form) with the same name and  checked.  This guarantees that the name will always be posted and always as an array, but also assumes the application is looking for specific values or ignoring empty values (a checkbox/radio button with no value= still has no real value). </p>
<p>A second alternative is to send a hidden field containing a list of checkbox names, then at the host pre-process the input to interrogate this hidden list of checkbox names and add any with an appropriate &#8220;missing&#8221; indicator to the session environment, this assumes that variables can be added transparently at the host to the POSTed stream.</p>
<p>A third alternative is to use javascript on the client to use the hidden field mentioned in alternative #2 above when the form is submitted to interrogate the form fields and if none of the boxes are checked then create a dummy entry and attach it to the DOM to be sent (still assumes that the application wants a real value=).</p>
<p>The menu form shown earlier is a classic example of what should be a checkbox array where each value= is the name e.g.<br />
type=&#8221;checkbox&#8221; name=&#8221;sauces&#8221; value=&#8221;Apricot Sauce&#8221;<br />
type=&#8221;checkbox&#8221; name=&#8221;sauces&#8221; value=&#8221;Marinara Sauce&#8221;<br />
etc.<br />
so the application should be iterating through the array and checking the presence of the array before starting the iteration.</p>
<p>There are many pros and cons to the W3C &#8220;successful&#8221; behaviour of an individual HTML element in a POST or GET, but the fact remains we should be writing defensive code, after all we never know when a &#8216;bot may be trying to infiltrate our site.</p>
<p>Phil</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: InsanP</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-113346</link>
		<dc:creator>InsanP</dc:creator>
		<pubDate>Wed, 30 Jun 2010 03:28:14 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-113346</guid>
		<description>Works like charm! Thank you so much!
Ordinary checking by using isset() is not enough in my case since I&#039;m dealing with sessions and need to refresh the value of all checkboxes!

Thanks again!</description>
		<content:encoded><![CDATA[<p>Works like charm! Thank you so much!<br />
Ordinary checking by using isset() is not enough in my case since I&#8217;m dealing with sessions and need to refresh the value of all checkboxes!</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-103932</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Sat, 24 Apr 2010 14:03:30 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-103932</guid>
		<description>I&#039;ve been doing this for years, except if you add 
  
at the END of the document instead of the front then it also works for arrays of checkboxes, you can also use it for arrays of radio buttons.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been doing this for years, except if you add </p>
<p>at the END of the document instead of the front then it also works for arrays of checkboxes, you can also use it for arrays of radio buttons.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dazbert</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-95303</link>
		<dc:creator>Dazbert</dc:creator>
		<pubDate>Tue, 25 Aug 2009 14:48:30 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-95303</guid>
		<description>Great answer to a tricky problem. By far the most elegant workaround I could find, and seems like less of an overhead than querying the database and cross-referencing to the text file I&#039;m storing the data in.

Thanks!</description>
		<content:encoded><![CDATA[<p>Great answer to a tricky problem. By far the most elegant workaround I could find, and seems like less of an overhead than querying the database and cross-referencing to the text file I&#8217;m storing the data in.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garrett</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-94926</link>
		<dc:creator>Garrett</dc:creator>
		<pubDate>Sun, 09 Aug 2009 17:43:21 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-94926</guid>
		<description>Freakin&#039; awesome. THANK YOU. Was wracking my brain trying to figure out what to do when I have a dynamic amount of checkboxes and how to &#039;track&#039; how many were there. Great idea, worked perfectly.</description>
		<content:encoded><![CDATA[<p>Freakin&#8217; awesome. THANK YOU. Was wracking my brain trying to figure out what to do when I have a dynamic amount of checkboxes and how to &#8216;track&#8217; how many were there. Great idea, worked perfectly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Justin</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-79325</link>
		<dc:creator>Justin</dc:creator>
		<pubDate>Wed, 15 Oct 2008 00:33:10 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-79325</guid>
		<description>This is a lifesaver, scratching my head on this for about an hour wondering if I could ever figure out how to account for something that doesn&#039;t exist.  I knew there had to a simple, non-bloated code solution where you account for every checkbox, and this is it! Thanks</description>
		<content:encoded><![CDATA[<p>This is a lifesaver, scratching my head on this for about an hour wondering if I could ever figure out how to account for something that doesn&#8217;t exist.  I knew there had to a simple, non-bloated code solution where you account for every checkbox, and this is it! Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ozh</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-74240</link>
		<dc:creator>Ozh</dc:creator>
		<pubDate>Sat, 20 Sep 2008 07:46:19 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-74240</guid>
		<description>Carbonize » This works fine as long as you know how many checkboxes you have in the submitted form. The trick is when you *don&#039;t* know.</description>
		<content:encoded><![CDATA[<p>Carbonize » This works fine as long as you know how many checkboxes you have in the submitted form. The trick is when you *don&#8217;t* know.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stratosg</title>
		<link>http://planetozh.com/blog/2008/09/posting-unchecked-checkboxes-in-html-forms/comment-page-1/#comment-74123</link>
		<dc:creator>stratosg</dc:creator>
		<pubDate>Fri, 19 Sep 2008 21:24:50 +0000</pubDate>
		<guid isPermaLink="false">http://planetozh.com/blog/?p=977#comment-74123</guid>
		<description>nothing is wrong :) it&#039;s just another way to do it ;)</description>
		<content:encoded><![CDATA[<p>nothing is wrong :) it&#8217;s just another way to do it ;)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

