Question

I have a form that submits data. Let's for example say it submits this data:

    Age     => 23
    Gender  => Male 
    Country => UK

Now I need to store this in a session array, that I have already made. BUT, this array already exists AND contains more fields than are given by the first form. For example, this is what the session array could look like:

    Age => Value
    Gender => Value
    Country => Value
    State => Value
    Language => Value

As you can see, only the first 3 are given with the first form. I would like a for each loop that detects which values are given (in this case: age, gender and country) and then place those values within the session array.

For example, the next form would give the information:

    State => Value
    Language => Value

I've been trying to wrap my head around this, but I just can't find a solution.. :/

Was it helpful?

Solution

Something like:

$valid_fields = array('Age', 'Gender', 'Country', 'State', 'Language');
foreach ($_POST as $key => $value) {
    if (in_array($key, $valid_fields)) {
        $_SESSION[$key] = $value;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top