checkbox key/value pairs missing from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

checkbox key/value pairs missing from array

Post 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";};?>/>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: checkbox key/value pairs missing from array

Post 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.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: checkbox key/value pairs missing from array

Post by rhecker »

Tasairis, with all due respect, you have simply restated the problem but not offered a solution. But thanks for your effort.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: checkbox key/value pairs missing from array

Post by requinix »

Sorry. I guess you wanted the solution, huh?
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: checkbox key/value pairs missing from array

Post by rhecker »

Yes, strange as it may seem, I am hoping for a solution.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: checkbox key/value pairs missing from array

Post 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';
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: checkbox key/value pairs missing from array

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: checkbox key/value pairs missing from array

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rhecker
Forum Contributor
Posts: 178
Joined: Fri Jul 11, 2008 5:49 pm

Re: checkbox key/value pairs missing from array

Post 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.
Post Reply