pho form pass on/off values


i have added a checkbox to my form but it does not submit values when it is not selected. this causes a problem as i need a value for either on or off. i tried including  a hidden field and then use javascript to disable it but this does not work as there are mutliple checkboxes that are created dynamically.

i know that a select input would work but i want it to look like a button either with a green check mark on it or a red x

is there some way of accomplishing this?
J NUnicorn wranglerAsked:
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
As you may have suspected, you are not the first to encounter the issues with checkboxes.  See if this article helps.
A checkbox will only pass a value if it is checked else it is ignored.
Maybe a radio button would be better here, so you have a constant state.
Add an hidden input type text, on click of the checkbox set the value of the hidden input to 0/1 depending on the state of the checkbox - is this maybe something you can do?
Dave BaldwinFixer of ProblemsCommented:
You can not get an 'off' value from an unchecked checkbox, it is simply not sent.  The standard way of doing this is:
if (!isset($_POST['chkbox']))  $chkbox = ''; else $chkbox = $_POST['chkbox'];

Open in new window

At the top of all my form processing scripts, I have lines like this that set default values for all of the possible POST values.  In the case of a checkbox, anything but 'on' means 'off'.  Although I always put a 'value' attribute in my checkbox statements so I don't get just 'on' anyway.  This below will return '1' instead of 'on'.
<input type="checkbox" name="box1" value="1" />

Open in new window

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

J NUnicorn wranglerAuthor Commented:

i did this but i cannot get it into the post array which i need it as a 'placeholder'

esentially i have tried to do this

				$_POST['delete_photo'] ="no";
				$_POST['delete_photo'] ="delete";

Open in new window

and my checkbox looks like
<input type="checkbox" class="myinput large custom" title="delete image" id="delete_photo[]" name="delete_photo[]" value="delete" />

Open in new window

GaryConnect With a Mentor Commented:
Something like this (unchecked)
<input type="checkbox" class="myinput large custom" title="delete image" class="delete_photo" value="delete" /> 
<input type="hidden" class="delete_photo_hidden" name="delete_photo[]" value="0" /> 


Open in new window

Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
If you have value="delete" in the input, you will never get a value of 'on'.  You only get 'on' when there is no value attribute in the input statement.

In addition, I never use the $_POST array for processing.  I create a separate set of variables as I showed above that will have defined default values.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.