Jump to content

Why doesn't this array update if no checkbox is selected?


Exoon

Recommended Posts

Hi,

 

Ive got this form:

 

xky8B.png

 

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.
Link to comment
Share on other sites

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 by Exoon
Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

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 by Exoon
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.