Jump to content

Session variable not sticking


wobblyfrogs

Recommended Posts

Am stuck again...

 

the aim is to put items in a cart and display them on the checkout page.

I was then going to write some code to multiply the 'amount of items' by their prices.

The items and their amounts transfer fine and display nicely, BUT

the 'Price session variable' does not seem to be carrying over to the checkout page???

:shrug:

Any suggestions...please...

 

//THE PHP to add stuff to the cart from various pages

session_start();

$PHPSESSID = session_id();

//check to make sure the session variable is registered

 

$item=$_POST['item'];

$amount=$_POST['amount'];

$_SESSION['price']=$_POST['price'];

 

$cartItems=$_SESSION['cartItems'];

 

if ($_POST['submit']=="Add to cart")

  {

   

if(!isset($cartItems))//check for array

{

$cartItems[$item]=$amount;//  eg gym = 2. 12.50 =2

}

else

{

foreach($cartItems as $k => $val) //for each arrya as a temporray value

{

if($item==$k)

{

$cartItems[$k]+=$amount;

$total=1;

}

}

if(!$total)

{

$cartItems[$item]=$amount;

}

}

     

$_SESSION['cartItems']=$cartItems;

 

}

?>

//THE FORM that adds the stuff

<form method="post" action="<?=$_SERVER['PHP_SELF']?>" >

          <input type="hidden" name="item" value="Buzzy Bee"  />

            <input type="hidden" name="price" value="34.99"  />

       

          <input type="hidden" name="amount" />

          <br />

          <input type="hidden" name="PHPSESSID" value="$PHPSESSID">

       

        </form>   

 

//THE CHECKOUT PAGE

<?php

$price= $_SESSION['price'] ;

if(isset($_SESSION['price'])){

echo"yes";}

else{echo"no";}//This is just to check that the variable was passed, and it always display "no"

 

if(isset($cartItems))

{

  echo "<strong><p>Your cart contains:</p></strong>";

{

    foreach($cartItems as $k => $val)

      {

    echo $k." ".$val."<br>";

}

}

}

 

   

?>

Link to comment
https://forums.phpfreaks.com/topic/180058-session-variable-not-sticking/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.