I'm trying to throw some values into a session variable so I can later use it in a shopping cart, but I'm not getting it, the values are id
and quantidade
.
What I did was this:
The form:
<form method="get" action="addtocart.php">
<div class="product-quantity">
<span>Quantity:</span>
<input type="hidden" name="id" value="<?php echo $prodr['id']; ?>">
<input type="text" name="quant" value="1">
</div>
<div class="shop-btn-wrap">
<input type="submit" class="button btn-small" value="Add to Cart">
</div>
</form>
session_start(); if(isset($_GET) && !empty($_GET)){ $id = $_GET['id']; $quant = $_GET['quant']; if (isset($quant) && !empty($quant)) { $quant = $_GET['quant']; } else { $quant = 1; } $_SESSION['cart'][$id] = array("quantity" => $quant); print_r($_SESSION['cart'][$id]); exit; header('location: cart.php'); } else { header('location: cart.php'); }
The variables $id
and $quant
have values, but when displaying print_r()
nothing is displayed.
As suggested, follow test results:
print_r($_GET); Array ( [id] => 21 [quant] => 1 )
print_r($_SESSION); Array ( [cart] => 1 [id] => Array ( [19] => 19 ) [quanr] => Array ( [19] => 1 ) [quant] => Array ( [19] => 1 ) )
In this second print_r
it appeared this [quanr]
that has nothing to do and I do not know where it came from.