0

I've got a page (dest.php) as part of a php session, every time a form on another page (source.php) submits to this one, I need to add the strings from the POST as another element in a $_SESSION variable.

So the idea is that I'll get several POST variables (['name'], ['gender'], etc) and need to add them to corresponding $_SESSION variable arrays, so the first time the form is submitted, I'll have $_SESSION['name'][0] = $_POST['name'] and each subsequent submit from source.php will send new $_POST variables, and on dest.php, I need to add to the $_SESSION arrays such that end($_SESSION['name']) = $_POST['name'] at all times.

I have a some php later in dest.php that is supposed to echo all the $_SESSION variables out in a table, but it's currently treating some of the strings different than others.

<table>    
<?php
for($cntr = 0; $cntr < count($_SESSION['name']); $cntr++)
{
   echo '<tr><td><input type="button" name="' . $cntr . '" value="edit"></td><td>';
   echo $_SESSION['name'][$cntr] .' adopted a ' . $_SESSION['gender'][$cntr] . ', ' . $_SESSION['colors'][$cntr];
   echo $_SESSION['species'][$cntr] . ' when visiting ' . $_SESSION['city'][$cntr] . '!' ;
   echo '</td></tr>';
}
?>
</table>

For reference, the form is:

<form action="petSentence.php" method="POST" accept-charset="UTF-8">
    <table><tr><td>
    Your name:  <input type="text" name="name"></td></tr><tr><td>
    <bold>Pick a city</bold></br>
        <input type="radio" name="city" value="Portland"> Portland<br>
        <input type="radio" name="city" value="Seattle"> Seattle<br>
        <input type="radio" name="city" value="Corvallis"> Corvallis</td><td>
    <bold>Pick the color(s)</bold>
        <input type="checkbox" name="colors[]" value="brown"> brown<br>
        <input type="checkbox" name="colors[]" value="red"> red<br>
        <input type="checkbox" name="colors[]" value="green"> green<br>
        <input type="checkbox" name="colors[]" value="blue"> blue</td></tr>
    <tr><td>
    Gender: <select name="gender">
        <option value="male">Male</option>
        <option value="female">Female</option>
    </select></td><td>
    What Species: 
        <select name="species">
        <option value="cat">cat</option>
        <option value="dog">dog</option>
        <option value="bird">bird</option>
        <option value="chinchilla">chinchilla</option>
    </select></td></tr>
    <tr><td><input type="submit" value="Submit"></td></tr>
    </table>
</form>

and an example of how I'm building the $_SESSION arrays:

if(isset($_SESSION['name']))
{
   array_push($_SESSION['name'], $_POST['name']);
}
else
{
   $_SESSION['name'] = $_POST['name'];
}
if(isset($_SESSION['gender']))
{
    array_push($_SESSION['gender'], $_POST['gender']);
}
else
{
    $_SESSION['gender'] = $_POST['gender'];
}  

Except the ['name'] variable is printing as expecting, but the ['gender'] array prints as if it's just a big character array.

3
  • Simplify: $_SESSION['name'][] = $_POST['name']; etc... Commented Jul 2, 2016 at 17:14
  • I actually did try that in one iteration, but it didn't fix the underlying problem, so I stuck with what I already had until I fix the issue, then I'll worry about simplifications/optimizations. Commented Jul 2, 2016 at 17:16
  • @AbraCadaver, if you write that up as an answer, I'll accept it right away. I went back to try that simplification and it caused problems until I ended the session and started it over, now it's working great. array_push() is pushing a c-string (effectively), but your simplification is pushing a string to a single element. Commented Jul 2, 2016 at 17:57

2 Answers 2

2

Simplify. No need for the ifs and elses:

$_SESSION['name'][]   = $_POST['name'];
$_SESSION['gender'][] = $_POST['gender']; 
Sign up to request clarification or add additional context in comments.

3 Comments

Is it advisable to do this, keep on adding to session variable from a form submit?
Depends. I guess a shopping cart or multi page test or something.
Probably not if there were potential memory risks on the server, at a certain point, you ought to be populating a db, but this was a small assignment for a class where the task was to have data persist during a session, so it works for me where there isn't a risk of some mass-population
1

You need to make sure that the $_SESSION["gender"] and $_SESSION["name"] are initially created as arrays. You should also make sure that they are not just set but are arrays before you append:

e.g.

function append_to_session_array($var_name)
{
  if (is_array($_SESSION[$var_name]))
  {
    $_SESSION[$var_name][] = $_POST[$var_name];
  }
  else
  {
    $_SESSION[$var_name] = array($_POST[$var_name]);
  }
}

foreach (array("gender","name") as $v)
{
  append_to_session_array($v);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.