pho form pass on/off values

Hi,

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?
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
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.
Else
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?
0
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

0
J NUnicorn wranglerAuthor Commented:
Hi,

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

	if(!isset($_POST['delete_photo'])){
				$_POST['delete_photo'] ="no";
				
			}else{
				$_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

0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

GaryCommented:
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" /> 

<script>
$(".delete_photo").click(function(){
     if($(this).is(":checked")){
          $(this).next(".delete_photo_hidden").val('1')
     }else{
          $(this).next(".delete_photo_hidden").val('0')
     }
})

Open in new window

0
Dave BaldwinFixer 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.
0
Ray PaseurCommented:
As you may have suspected, you are not the first to encounter the issues with checkboxes.  See if this article helps.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_5450-Common-Sense-Examples-Using-Checkboxes-with-HTML-JavaScript-and-PHP.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.