tags:

views:

178

answers:

2

I have a form with check boxes in it. I didn't set the value of the check boxes I always thought the value was either 'on' or '' (empty). I have the value 'on' passing to the database. The problem is when I unchecked the box it isn't changing the value in the database to '' (empty). The rest of the values of the form, such as input boxes are being changed. Here is my code on my check boxes

<input  type="checkbox" name="form3_one_2" <?php if($form3_one_2 == 'on'){print 'checked="checked"';}?> />

Am I supposed to have an value put into the form???

A: 

If you want to write it as selected on the browser, as it looks like you are, use

<input name="foo" selected="selected" ... />

If you want to get the result of the checkbox, it should indeed be "on" when you pass it via a form (or querystring, if you're using GET).

Rushyo
What is the 'selected' attribute???
strager
Ah. I think checkboxes use checked="checked" instead. Selected would apply to a radio box (or anything else you can select). It means that is it is selected and/or checked :)
Rushyo
+1  A: 

You should keep track of the checkbox(es) in the form. When the form is posted, do something like

<?php

if( 'POST' == $_SERVER[ 'REQUEST_METHOD' ] )
{
  /* for security reasons just check
   * if it isset and set it to 'on' yourself
   * never trust users input, always validate / sanitize
   */
  $form3_one_2 = isset( $_POST[ 'form3_one_2' ] ) ? 'on' : '' ;
}

?>

By the way: Where is your $form3_one_2 coming from anyway? I have a feeling you are still working with the ini setting register_globals on, which is also a huge security hole from the jurassic age. Be sure to program for register_globals set to off.

fireeyedboy
I'm not sure what your saying. First of all security isn't a problem! I'm not always good at verbalizing what I'm thinking so let me try again. The value 'on' is being put in the data base. (This works)My problem is I want the user to be able make updates to the form.In order for them to read their old information I put this code in the form<?php if($form3_one_2 == 'on'){print 'checked="checked"';}?>this populates the form with the info from the data base.If they uncheck the form and resubmit it then the value 'on' is still in the data base. (That is my problem)
I named the check box $form3_one_2 to keep track of each box in each section of the form. I have over 200 checkboxes in the form.
I understand your problem fine. I think it's my response that needs clarification. What I ment to say is, that on the receiving end (the PHP script that handles the submitted form), you need to be aware of what checkboxes were present in that form. Because no value is submitted if a checkbox is unchecked. So, the only way to know if a checkbox is unchecked, is to test whether a value is lacking. If no value is received for a checkbox, which you know exists in the form, then the checkbox was unchecked.That is what$form3_one_2 = isset( $_POST[ 'form3_one_2' ] ) ? 'on' : '' ;does.
fireeyedboy
So, the receiving end needs to be aware of all possible checkboxes, so it can test the existence of a value for them. If no value is present, then the checkbox was (left) unchecked.
fireeyedboy
I think I understand I'm just not sure where to add the code. I have 9 forms using the same code and some of the forms have 40 check boxes. Here is part of my code:if(!empty($form_id)){ $sql = "UPDATE ".$form." SET "; foreach($_POST as $key => $value){ if(($key != 'submit_'.$form ) } } $sql = substr($sql, 0, -1); $sql .= " WHERE ".$applicant_id." = $applicant_id"; $result = mysql_query($sql,$db) or die(mysql_error(). "<br />SQL: $sql"); }
When I print out the Querry to the data base then it tells me that the value is being passed.