1

I have a form that allows users to enter a name and an associated date of birth.

The form allows users to dynamically add more names and date of births.

I would like to push this data to an associated array $_SESSION variable and then loop through it.

<form action="page.php">
  <input type="text" name="child[0][name]" value="Name">
  <input type="text" name="child[0][dob]" value="Date of Birth">
  <input type="submit" value="Submit">
</form>

//trying to save the posted data to a SESSION
$_SESSION['children'] = [];

if (isset($_POST['child'])) {
    foreach ($_POST['child'] as $value) {
       array_push($_SESSION['children'], $value['name']);
       array_push($_SESSION['children'], $value['dob']);
    }
}

What would the loop from the SESSION look like to get my data to read:

Peter Smith born on 11/11/1900
Sally Smith born on 11/22/2222

When I print_r($_SESSION):

Array ( [0] => Peter Smith [1] => 11/11/1900 [2] => Sally Smith [3] => 11/22/2222 )
4
  • don't forget the indices, child[0] so that each can be grouped Commented Jul 4, 2016 at 22:57
  • 1
    Your array organization seems like a bad idea. You shouldn't put the name and DOB in separate entries in the array, you should combine them into a single object or associative array. Commented Jul 4, 2016 at 22:59
  • 2
    why do you need a loop for? just straight assign it $_SESSION['children'] = $_POST['child'] Commented Jul 4, 2016 at 23:00
  • @FirstOne go on ahead, no prob, no need to attribution or anything Commented Jul 4, 2016 at 23:12

2 Answers 2

1

For your current session value, you'd go like this:

$for($i=0; $i<count($_SESSION['children']); $i+=2){
    echo $_SESSION['children'][$i] . ' born on ' . $_SESSION['children'][$i+1] . '<br>';
}

It would be better if you just kept the keys for a more precise code. Take a look:

// .. code ..
if (isset($_POST['child'])) {
    // check if it's not created yet
    if(!isset($_SESSION['children']){
        $_SESSION['children'] = array();
    }
    // Adds (without replacing) the values from post to session
    $_SESSION['children'] = array_merge($_SESSION['children'], $_POST['child']);
    // Display the new session data
    foreach($_SESSION['children'] as $v){
        // better access using "name" and "dob"'s keys
        echo $v['name'] . ' was born on ' . $v['dob'] . '<br>';
    }
}

Note that this code keeps the previous $_SESSION['children'] value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys! That super simplified it and I was able to easily loop.
0

First, only initialise $_SESSION['children'] if it is not already an array

if (!array_key_exists('children', $_SESSION)) {
    $_SESSION['children'] = [];
}

Then, simply merge $_POST into $_SESSION['children']

$_SESSION['children'] = array_merge($_SESSION['children'], $_POST);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.