0

I'm trying to make a page that takes a form submission and adds the contents as a new value in the $_SESSION() array, what seems to be happening though is the value is being overridden.

The form has 3 text inputs named a, b and c and refreshes the page on submission. What tells me it's being replaced is $_SESSION[0] will display 1 2 and 3 as defined below, then the next row defined by $_POST will be the same but with the array values replaced by the last submitted values rather than adding the last submitted as another row.

<form action="test2.php" method="post">
<input type="text" name="a">
<input type="text" name="b">
<input type="text" name="c">
<input type="submit" value="Submit">
</form>

<?php
    if (isset($_POST['a']))
    {
     $a = $_POST['a'];
     $b = $_POST['b'];
     $c = $_POST['c'];
     $order = array('a' => $a, 'b' => $b, 'c' => $c);
     $_SESSION[0] = array('a' => 1, 'b' => 2, 'c' => 3);
     $_SESSION[] = $order;
     $count = count($_SESSION);
     for ($i = 0; $i < $count; $i++) {
      echo "w: " . $_SESSION[$i]['a'] . "\n";
      echo "h: " . $_SESSION[$i]['b'] . "\n";
      echo "p: " . $_SESSION[$i]['c'] . "\n";
      echo "<br />";
      }
    }
?>

Would be extremely grateful for any help, Thanks

1
  • can you explain with an example what you want as a result? your text isn't really clear... Commented Nov 18, 2010 at 13:15

2 Answers 2

3

It looks to me like you are trying to add a new array to a new $_SESSION var each time the form is submitted. The method you are using will only add the value to the $_SESSION array for that page load - it won't actually be in the $_SESSION array! Confusing right? So either of these won't work...

$_SESSION[] = 'value or array';
$_SESSION[1] = 'some other stuff';

But this will, due there being text in the $_SESSION key (and don't forget to start the session).

session_start();
$next = count($_SESSION) + 1;
$next = 'foo' . $next;
$_SESSION[$next] = 'bar' . $next;

This will generate the below for "print_r($_SESSION)".

Array ( [foo1] => barfoo1 [foo2] => barfoo2 [foo3] => barfoo3 [foo4] => barfoo4...
Sign up to request clarification or add additional context in comments.

2 Comments

You are a god amongst men! I don't suppose you have a link or anything explaning as to why the lack of text in the $_SESSION key was stopping it from working so I can educate myself and not run into it again? Thanks again.
Its to do with $_SESSION being a superglobal, and so every key must also be a valid variable name (i.e. $1 is an invalid variable). See the forth comment down here uk.php.net/manual/en/reserved.variables.session.php#85147
2

Most simplest way of adding form values would be

$_SESSION['form'] = $_POST; //once the form is posted

Then access the values using

$_SESSION['form']['fieldname'];

2 Comments

Okay that could work, but what about adding a second, third etc form submission, for example $_SESSION('form1', 'form2', 'form3'....) this is why I used $_SESSION[] so it adds each submission as another array of values but I don't understand why it replaces the previous values?
@Bigrob, Cant you assign different form names in session like $_SESSION['frm_News'], $_SESSIOn['frm_artitle']

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.