I am currently coding my way through an online shop and I am stuck on form submitting value to another page using the $_POST variable. So let me explain in code....
This page, named "product.php", holds a form that lists different sizes in a comma separated format to radio button list and is supposed to submit the product ID and the size (selected by user) that will be inserted to "cart.php" that is eventually the shopping cart page.
Code: Select all
<form method="post" name="myform" action="cart.php">
<?php
//get sizes from field size DB
$sizes = $row_getProduct['size'];
$options = explode(',', $sizes);
?>
<?php
foreach ($options AS $option)
{
?>
<input name="br" type="radio" onClick="document.myform.formVar.value='<?php echo $option?>'"><?php echo $option?><br><br>
<?php
}
?>
<input type="hidden" name="myform" value="<?php echo $option?>">
<input type="hidden" name="myform" id="pid" value="<?php echo $id?>"/>
<input type="submit" name="button" value="Add To Cart"/>
</form>Code: Select all
<?php
//size selected by user
$myvar = $_POST['myform'];
echo "Your Size: ".$myvar;
?>
//user adding items to cart
if(isset($_POST['pid'])){
$pid=$_POST['pid'];
$wasFound=false;
$i=0;
//if cart session variable is not set or cart array is emtpty
if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
//If cart is empty or not set
$_SESSION["cart_array"]=array(0=>array("item_id"=>$pid,"quantity"=>1));
}else{
//run if the cart has at least one item
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key, $value)= each($each_item)){
// finding the match
if($key == "item_id" && $value == $pid){
//that item is in cart already so let's adjust its quantity using array_spliced()
array_splice($_SESSION["cart_array"],$i-1,1,array(array("item_id"=>$pid, "quantity" => $each_item['quantity']+1)));
$wasFound=true;
if(isset($_POST['size'])){
foreach($_POST['size'] as $checkbox){
echo $checkbox . ' ';
} }
}//close IF
}//close while
}//close for each
if($wasFound==false){
array_push($_SESSION["cart_array"], array("item_id" =>$pid,"quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>