submit comma separated value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asch
Forum Newbie
Posts: 3
Joined: Mon Mar 19, 2012 2:50 am

submit comma separated value

Post by asch »

Hi guys I am new here and I am in search of some help!!
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>
Now this little code here (cart.php), contains an array to hold every item added to cart by the user, that is working fine until I try to insert the selection of size selected by user of which gives me "Your Size: 84" ...(84 is the product ID) and leaves the array empty! but as soon as i remove the form for size submition the array works as it should!

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();
}
?>
Hope my explanation is clear enough...if not please ask for explanation again don't worry!!....ANY SUGGESTIONS GUYS?!?! I tried for about 2 hours solving the issue but got no result!!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: submit comma separated value

Post by social_experiment »

The explanation is a bit difficult to understand;
asch wrote:Now this little code here (cart.php), contains an array to hold every item added to cart by the user, that is working fine until I try to insert the selection of size selected by user of which gives me "Your Size: 84" ...(84 is the product ID) and leaves the array empty! but as soon as i remove the form for size submition the array works as it should!
Which array are you refering to $_SESSION['cart_array'] or a different array?

Code: Select all

<?php
<input type="hidden" name="myform" value="<?php echo $option?>">
?>
A note on this line of code : $option in this case contains the value of the last item in the array. What do you mean by "selection of size"?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
asch
Forum Newbie
Posts: 3
Joined: Mon Mar 19, 2012 2:50 am

Re: submit comma separated value

Post by asch »

hi social_experiment,

yeah I know its a bit difficult to understand but its even harder for me to explain! :roll: ...anyway you just answered one of my questions by your statement:
A note on this line of code : $option in this case contains the value of the last item in the array. What do you mean by "selection of size"?
Regarding the $option value....how can I give a group of radio buttons a value from the array ???

Code: Select all

$options = explode(',', $sizes);
Thank You Mate!
Post Reply