2

I am trying to get an array to pass in a session variable from one page to another. I am setting the session variable equal to my the $_POST data collected from my form. Printing the variable for test shows me that it is getting set initially on Page 1.

Page 1

session_start(); // start up your PHP session! 

if(isset($_POST['submit']))
{


unset($_POST['submit']);
$_SESSION['userdata'] = $_POST;
$userqty=$_POST;
print_r($_SESSION['userdata']);

On Page 2 I am trying to set it equal to another variable. Again I test to see if the array has passed to page 2 but it doesn't and I get an error for not having a valid array for the array_sum function

session_start(); // start up your PHP session!

$_SESSION['userdata'] = $userqty;

print_r($_SESSION['userdata']);

print_r($userqty;);

$userqty_total=array_sum($userqty);

Any help would be greatly appreciated.

1
  • You're essentially setting $_SESSION['userdata'] to null on page 2. Switch the assignment order: $userqty = $_SESSIOn['userdata']; Commented Jul 31, 2012 at 18:56

3 Answers 3

3

You're setting $_SESSION['userdata'] to the value of $userqty, which is uninitialized.

Change your first line to:

session_start();
$userqty = $_SESSION['userdata']
Sign up to request clarification or add additional context in comments.

Comments

2

checkout serialize and unserialize functions in php.

EDIT: actually you need not convert the array to string and convert it back to array. You can use it as it is.

on first page:

$_SESSION['data'] = $_POST;

on second page:

$temp = $_SESSION['data'];

now $temp has the $_POST contents of page 1.

Comments

0

in page1:

 session_start();
 $array  = array("appel","car","human");
 $_SESSION['array'] = $array ;

in page2:

for ($i = 0; $i < count($nom); $i++) {
        echo $array[$i] . ' ';
   }

1 Comment

Welcome to SO, To make it easier to understand your answer, please explain briefly what you did here.

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.