0

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.. :/

Jofairden
  • 95
  • 9
  • check this http://stackoverflow.com/questions/2616540/can-i-use-array-push-on-a-session-array-in-php – Lord Zed Feb 28 '14 at 19:12

1 Answers1

0

Something like:

$valid_fields = array('Age', 'Gender', 'Country', 'State', 'Language');
foreach ($_POST as $key => $value) {
    if (in_array($key, $valid_fields)) {
        $_SESSION[$key] = $value;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hey, it doesn't seem to be working. I use that same code, with a different array name but here's what I get when I dump the $_POST array: `array(6) { ["mysqlname"]=> string(4) "root" ["mysqlpass"]=> string(4) "root" ["mysqlhost"]=> string(9) "localhost" ["mysqldatabase"]=> string(3) "pfs" ["mysqlport"]=> string(4) "3306" ["seed"]=> string(32) "837ec5754f503cfaaee0929fd48974e7" }` and this is what my $_SESSION looks like: `The value of $_SESSION['mysqlname'] is '' The value of $_SESSION['mysqlpass'] is '' `.... not full.. but you get the idea.. it stays empty – Jofairden Feb 28 '14 at 21:37
  • That makes no sense. `$_POST` is always initialized with the form field values. I don't see how all the MySQL stuff could be getting into it, you must have some other code in your script that's doing this. – Barmar Feb 28 '14 at 21:40
  • Oh wait, I just noticed I did stuff in the wrong order. Let me remake my code and then I'll come back to you with my findings. – Jofairden Feb 28 '14 at 21:41
  • You're misunderstanding how `var_dump` displays values. That means it has the value `"root"`, which is a string that's 4 characters long. – Barmar Feb 28 '14 at 21:42
  • Where is the `Age`, `Gender`, `Country` stuff that you said was in your form input? – Barmar Feb 28 '14 at 21:43
  • Yeah sorry, I use different ones in the real code. I should've just put them in the OP. any way, I cleaned up the code, and here's what I get now: `The value of $_SESSION['0'] is 'mysqlusername' The value of $_SESSION['1'] is 'mysqlpass' The value of $_SESSION['2'] is 'mysqlport' The value of $_SESSION['3'] is 'mysqlhost' The value of $_SESSION['4'] is 'mysqldatabase'` But those are the actual id's / names of the input fields. – Jofairden Feb 28 '14 at 21:52
  • Oh wait nevermind! You are absolutely right!! Something must've went wrong in the code, as I deleted stuff that I didn't need the code suddenly works perfect! Thanks a bunch! This is exactly the result I needed. – Jofairden Feb 28 '14 at 21:59