Page 1 of 1

checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 7:06 pm
by rhecker
The following is part of a code snippet someone from this forum once gave me which generates the key/value pairs from a form submission. The problem I now have is that checkbox pairs are only included if the value is positive. So if I unselect a checkbox which has previously been selected, it will not be included in the key/value pairs and thus will not update the database. I don't understand how to include the checkbox value when it is negative. I have also included an example of the code I use for checkboxes. Any help greatly appreciated.

Code: Select all

foreach ($_POST as $k=>$v){
$v = mysql_real_escape_string($v);
  $set .= $k." = '".$v."', ";
}
 
<input type="checkbox" name="abc" value="1" <?php if ($abc == "1") {echo " CHECKED";};?>/>
 

Re: checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 9:01 pm
by requinix
You have to figure out which checkboxes were unchecked yourself. You can't get it from $_POST.

One way would be to figure out which ones you presented on the form and "subtract" from that the ones that were checked.

Re: checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 10:11 pm
by rhecker
Tasairis, with all due respect, you have simply restated the problem but not offered a solution. But thanks for your effort.

Re: checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 10:43 pm
by requinix
Sorry. I guess you wanted the solution, huh?

Re: checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 11:36 pm
by rhecker
Yes, strange as it may seem, I am hoping for a solution.

Re: checkbox key/value pairs missing from array

Posted: Wed Dec 09, 2009 11:45 pm
by pcoder
I think, you can check like this before creating key=>val pair. Otherwise, you can't get the unchecked value at the POST.

Code: Select all

$_POST['abc'] = isset($_POST['abc'])?$_POST['abc']: 'Default Value';

Re: checkbox key/value pairs missing from array

Posted: Thu Dec 10, 2009 8:55 am
by rhecker
Pcoder, yes, I can also get the value if I make an explicit list of values in the mysql query like so:

abc = '$abc',

The above would apply a negative boolean to the value in the database...but the whole point is to have the key/value pairs built dynamically instead of hard-coded.

I'm surprised that the checkboxes with empty values are not being included in $_POST. If a form field type is "text" and the value is emply, it is still included in $_POST.

Re: checkbox key/value pairs missing from array

Posted: Thu Dec 10, 2009 11:06 am
by pickle
That's the nature of the beast - no way around it. You have to have a source of which fields to update that doesn't come from $_POST.

Re: checkbox key/value pairs missing from array

Posted: Thu Dec 10, 2009 11:37 am
by rhecker
I have discovered a workaround which works for me. By assigning hidden fields with the same names as the checkboxes giving them the value "" and placing them all at the beginning of the set of form fields, I am able to toggle the checkboxes on and off.

Code: Select all

<input type="hidden" name="abc" value="">
This is an acceptable solution for me because I have to explicitly name form fields anyway. It's only the processing of the form data that I want to be reusable.