Skip to content
Advertisement

PHP passing array in session variable

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.

Advertisement

Answer

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

Change your first line to:

session_start();
$userqty = $_SESSION['userdata']
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement