...

View Full Version : Sending a $_POST for an unchecked checkbox?



Mechphisto
11-23-2010, 06:52 PM
I'm setting up a for-each in an included file for all our Web pages, which converts all $_POST's into $_SESSION variables, so that if someone hits BACK while submitting a form, it's sure to repopulate the fields with what they typed.
(Code samples at the bottom.)

It works great, except if the submitter checks a checkbox, then goes back and decides to uncheck it, the $_SESSION remembers the checked value because an unchecked checkbox doesn't send a $_POST value.

Maybe I just can't think outside the box enough (heh), but I can't think of a way to send an empty value or unset the value and still keep the $_POST-to-$_SESSION process uncluttered with if's for specific checkbox verification.

Any ideas what I might try?

Here's a sample of a form:



<tr>
<?php unset($chk_flag);
if ($_SESSION['chk_contactback'] == "1") { $chk_flag = "checked"; }
?>
<td colspan="2"><input type="checkbox" name="chk_contactback" id="chk_contactback" value="1" <?=$chk_flag?> />
<label for="chk_contactback">check this box if you would like to be contacted back</label></td>
</tr>



And here's the ubiquitous PHP that gets loaded on the submission of any form:



// MAKE POSTs INTO SESSION VARS
// (to re-fill forms if the BACK button is used)
if (!empty($_POST)) {
foreach ($_POST as $k=>$v) {
unset($_SESSION[$k]);
$_SESSION[$k] = $v;
}
}


Thanks!

MattF
11-23-2010, 07:03 PM
Something like the following example should work:



$_SESSION['checkbox'] = ((isset($_POST['checkbox']) && !empty($_POST['checkbox'])) ? $_POST['checkbox'] : false);


The session var will still be set, but a quick:



if ($_SESSION['checkbox'])


will say whether it contains a value or not when you reference it.

Mechphisto
11-23-2010, 07:18 PM
Something like the following example should work:



$_SESSION['checkbox'] = ((isset($_POST['checkbox']) && !empty($_POST['checkbox'])) ? $_POST['checkbox'] : false);


The session var will still be set, but a quick:



if ($_SESSION['checkbox'])


will say whether it contains a value or not when you reference it.

Hmm, correct me if I'm wrong, but if I'm reading that right, that example would require me to place script that references that specific checkbox ID in the PHP that converts $_POST to $_SESSION values?

If so, that's exactly what I'm trying to avoid. That PHP is found in a separate file mentioned in an include() on every page -- so that EVERY form regardless of number and ID of the fields can benefit from being SESSION'ed.

...if I'm understanding your example right. :)
Thanks for the reply!

Rowsdower!
11-23-2010, 07:48 PM
Store all of your form inputs in an array within the $_SESSION variable ( $_SESSION['form_data'] ). Then, check each and every input element in your $_SESSION['form_data'] array against your $_POST variables submitted in a loop. It won't matter how many or how few the items may be. If they are empty in the $_POST array then destroy them in the $_SESSION array in the manner MattF suggested.

If your form is submitted across several pages, simply subdivide the $_SESSION['form_data'] array into arrays for each step ( $_SESSION['form_data']['step_1'] ). It seems to me that that should do the trick... but I'm pretty exhausted today so I may not be making complete sense.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum