{"id":977,"date":"2008-09-07T21:47:35","date_gmt":"2008-09-07T19:47:35","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=977"},"modified":"2008-09-07T21:49:34","modified_gmt":"2008-09-07T19:49:34","slug":"posting-unchecked-checkboxes-in-html-forms","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2008\/09\/posting-unchecked-checkboxes-in-html-forms\/","title":{"rendered":"Posting Unchecked Checkboxes in HTML Forms"},"content":{"rendered":"<p>I ran into an interesting problem, facing what is certainly an HTML limitation that had somehow never really occurred to me. The thing is: when you submit a form with empty fields, empty values are POSTed, except for checkboxes and radio buttons which are not posted at all, just as if they were no such field in the form.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"148\" src=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/09\/checkboxes.jpg\" alt=\"\" title=\"checkboxes\" class=\"aligncenter size-full wp-image-980\" srcset=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/09\/checkboxes.jpg 580w, https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/09\/checkboxes-300x76.jpg 300w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<p>For instance, this form:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">HTML<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"html4strict\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">form<\/span> <span style=\"color: #000066\">method<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;post&quot;<\/span> <span style=\"color: #000066\">action<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">input<\/span> <span style=\"color: #000066\">type<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;text&quot;<\/span> <span style=\"color: #000066\">name<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;input&quot;<\/span> <span style=\"color: #000066\">value<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">input<\/span> <span style=\"color: #000066\">type<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;hidden&quot;<\/span> <span style=\"color: #000066\">name<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;hidden&quot;<\/span> <span style=\"color: #000066\">value<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">input<\/span> <span style=\"color: #000066\">type<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;checkbox&quot;<\/span> <span style=\"color: #000066\">name<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;checkbox&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #66cc66\">\/<\/span><span style=\"color: #000000;font-weight: bold\">form<\/span>&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Submitting it, leaving empty values and not checking the checkbox, you would get the following $_POST array (notice the missing &quot;checkbox&quot;)<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #990000\">array<\/span> <span style=\"color: #009900\">&#040;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; <span style=\"color: #0000ff\">&quot;input&quot;<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">&quot;&quot;<\/span><span style=\"color: #339933\">,<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; <span style=\"color: #0000ff\">&quot;hidden&quot;<\/span> <span style=\"color: #339933\">=&gt;<\/span> <span style=\"color: #0000ff\">&quot;&quot;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#041;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>In short: <tt>&lt;input type=\"checkbox\" name=\"field\" value=\"something\"><\/tt> either gets submitted as <tt>field = \"something\"<\/tt> if checked, or doesn&#39;t exist if unchecked.<\/p>\n<p>My problem was that I needed to process an arbitrary number of checkboxes while being able to know wether a checkbox was left unchecked, that is I needed to have a value for <em>each<\/em> checkbox in the $_POST array, either blank\/off\/empty or checked\/on\/whatever.<\/p>\n<p>The solution I found was simple: before each checkbox, add a hidden field with the same name:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">HTML<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"html4strict\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">form<\/span> <span style=\"color: #000066\">method<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;post&quot;<\/span> <span style=\"color: #000066\">action<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">input<\/span> <span style=\"color: #000066\">type<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;hidden&quot;<\/span> <span style=\"color: #000066\">name<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;checkbox&quot;<\/span> <span style=\"color: #000066\">value<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #000000;font-weight: bold\">input<\/span> <span style=\"color: #000066\">type<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;checkbox&quot;<\/span> <span style=\"color: #000066\">name<\/span><span style=\"color: #66cc66\">=<\/span><span style=\"color: #ff0000\">&quot;checkbox&quot;<\/span>&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&lt;<span style=\"color: #66cc66\">\/<\/span><span style=\"color: #000000;font-weight: bold\">form<\/span>&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>This way, there is always a value for field <tt>checkbox<\/tt> in the $_POST array: either &quot;&quot; if the checkbox was unchecked, or &quot;on&quot; if it was checked. I&#39;ve uploaded a simple HTML form if you&#39;re curious to try: <a href=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/09\/post-unchecked-checkboxes.php\">regular way, without the &quot;special hidden&quot; fields<\/a>, or <a href=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2008\/09\/post-unchecked-checkboxes.php?hidden=true\">without the duplicate hidden fields<\/a>. Submit the form with all fields left to empty and see the resulting $_POST array.<\/p>\n<p>This simple yet effective and smart workaround was found by <a href=\"http:\/\/iamcam.wordpress.com\/2008\/01\/15\/unchecked-checkbox-values\/\">Cameron<\/a> and I really love it. (And the image above is by <a href=\"http:\/\/www.flickr.com\/photos\/mrjoro\/73409854\/\">Joey Rozier<\/a> from a restaurant where you apparently can build your perfect burger, seems fun:)<\/p>\n<p>This trick might or might now suit your need: for instance, client-side javascript serialization of the form could lead to something unexpected because of the duplicated fields. Also, this won&#39;t work with arrays of checkboxes (something like having multiple <tt>&lt;input type=\"checkbox\" name=\"field[]\"\/><\/tt>). But for the particular case I had to deal with, it was very perfect. Love it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into an interesting problem, facing what is certainly an HTML limitation that had somehow never really occurred to me. The thing is: when you submit a form with empty fields, empty values are POSTed, except for checkboxes and radio buttons which are not posted at all, just as if they were no such [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[329,2,265],"class_list":["post-977","post","type-post","status-publish","format-standard","hentry","category-published","tag-checkbox","tag-code","tag-html"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=977"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/977\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}