I'm trying to figure out a way to remove checkbox values from a SESSION
.
I have a form with a checkbox array, among other fields. When the form is submitted, the field values are stored in a SESSION
, so that if the user refreshes the page, the values stick.
However, if they then uncheck the checkboxes and re-submit the form, the checkbox values still stick because the checkbox name never gets posted in the POST
array, so I don't know which values to unset()
I'm trying to come up with a way to look into the SESSION
and remove anything that's not in POST
.
This is what I have so far, but it's just not working right because it's checking for a non-existent value in POST
. I think it's backwards, but I haven't been able to figure out how to reverse it.
foreach ($_POST as $post_key => $post_value) {
if (is_array($post_value)) {
$result = array_diff($_SESSION[$this->session], $_POST[$post_key]);
foreach ($result as $key => $value) {
if (($k = array_search($value, $_SESSION[$this->session])) !== false) {
unset($_SESSION[$this->session][$k]);
}
}
}
}
The POST checkbox array is basically
Array
(
[food] => Array
(
[0] => steak
[1] => chicken
)
[submit] => Submit
)
Of course, when no checkboxes are checked, it looks like this, so I don't know what to unset()
Array
(
[submit] => Submit
)