Exoon Posted October 30, 2013 Share Posted October 30, 2013 Hi, Ive got this form: Im using this to store my selections of a form in sessions so they can be recalled if the user goes back to another page of the form. The problem i have is it always has to have 1 selection, If i tick Jam, then untick Jam and tick butter that works fine, But if i uncheck everything then it doesn't clear the array. It always has to have at least 1 entry in the array for some reason? foreach($_POST as $key => $value) { $_SESSION[$key] = $value; } This is the checkbox code im using: <inputtype="checkbox name="<?php echo $course_menuname."extras[]"; ?>" <?php if(in_array("$extraitems", $_SESSION[$course_menuname.'extras'])) { echo "checked"; } ?> value="<?php echo $extraitems; ?>" id="checkbox"> These are the variables: $extraitems contains the "Toast Extras" words, such as Jam, Butter and Flora $course_menuname contains the course such as "breakfast_cereal", "breakfast_toast" then if it has the word extra it does checkbox underneath. Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 30, 2013 Share Posted October 30, 2013 try to force clear the array if everything is unchecked? if(all unchcked) unset($array); or you could try using some jquery and prevent the user from unchecking them all Quote Link to comment Share on other sites More sharing options...
Exoon Posted October 30, 2013 Author Share Posted October 30, 2013 (edited) i need to let them be able to uncheck them really, I can't figure out how to check if its empty then unset it in my foreach loop, the $key has "brreakfast_toastextras" and $value has "Jam" inside it. I tried this: foreach($_POST as $key => $value) { if( !isset($_POST[$key]) ) { unset($_SESSION[$key]); } else { $_SESSION[$key] = $value; } } It doesn't work though, any ideas? Edited October 30, 2013 by Exoon Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted October 30, 2013 Share Posted October 30, 2013 When the form is submitted, firstly destroy the session variable and then reset it. Sessions will persist until you destroy them. It would be better to store the post data under a specific session key as opposed to a direct copy of the post array so you can easily clear it. i.e // form submitted if(isset($_POST) && count($_POST)) { // clear session data unset($_SESSION['form_data']); // store session data $_SESSION['form_data'] = $_POST; } // just to see whats in my session I will dump it if(isset($_SESSION)) { print_r($_SESSION); } Quote Link to comment Share on other sites More sharing options...
Exoon Posted October 30, 2013 Author Share Posted October 30, 2013 (edited) I add your if statement to for loop and if i select nothing it still doesn't clear the session foreach($_POST as $key => $value) { // form submitted if(isset($_POST[$key]) && count($_POST[$key])) { // clear session data unset($_SESSION[$key]); // store session data $_SESSION[$key] = $value; } } Edit: I found this while looking on google and it seems to have worked i think, Incase anyone else having the same issue: <form> <input type='hidden' value='0' name='selfdestruct'> <input type='checkbox' value='1' name='selfdestruct'> </form> Edited October 30, 2013 by Exoon Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 30, 2013 Share Posted October 30, 2013 (edited) The problem is checkbox fields are only contained within the $_POST if they are checked. If they are not checked then they wont be in the $_POST array. If you used neil.johnson suggestion correctly you wont need to add a hidden form field. Edited October 30, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.