1

What my scenario is I have items in session when loading to this page.php,

now in this page.php on a form submit it posts some values to same page.php, And am trying to add post values to existing session values by the following code,

    if (isset($_POST['special'])) {
    for ($i = count($_SESSION['item']); $i <= count($_SESSION['item']); $i++) {
        $index = ++$i;
        $_SESSION['price'][$index] = $_POST['price'];
        $_SESSION['item'][$index] = $_POST['name'];

    }
}

Am doing the following for re-arranging the items(as I do have deleting particular items)..

$k=0;
for ($j = 1; $j <= count($_SESSION['item']); $j++) {
if ($_SESSION['item'][$j] != '') {
    ++$k;
    $itemName[$k] = $_SESSION['item'][$j];
    $itemPrice[$k] = $_SESSION['price'][$j];
    }
$_SESSION['item'] = $itemName;

$_SESSION['price'] = $itemPrice;

On echo of the content of $_SESSION['item'] am finding the newly added available in page.php, but when I move to next page the newly added session is alone missing.

5
  • 1
    this sounds very weird problem. is there any error shown in the next page? is the next page is out of your current domain? Commented Jun 4, 2014 at 11:01
  • Ya weird! its in the same domain. Commented Jun 4, 2014 at 11:12
  • 3
    are u doing session_start() on each page ? Try var_dump($_SESSION); Commented Jun 4, 2014 at 11:29
  • 2
    Are you aware that the variable $i is being incremented twice each time around the loop? Once via $index = ++$i; and once in the 'for' loop itself? Commented Jun 4, 2014 at 12:29
  • 1
    yeah, ryan is right, why to increment it twice? and check you have session_start(); in every page. Commented Jun 5, 2014 at 13:05

1 Answer 1

1

You need to use session_start() inside your script. If not then the session won't continue when moving between pages.

<?php

session_start();

// Your code

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

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.