Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

submit comma separated value

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

submit comma separated value

Postby asch » Sun Mar 18, 2012 10:19 am

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();
}
?>


ANY SUGGESTIONS GUYS?!?! I tried for about 2 hours solving the issue but got no result!!
asch
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Mar 18, 2012 9:48 am

Re: submit comma separated value

Postby asch » Sat Mar 24, 2012 8:37 am

Ok guys I know it was a bit difficult to understand so I will ask a more simpler question.....

How can I add values from an array into a group of radio buttons and be able to post the form with the selected value?

this is what I currently have:
Code: Select all
//$sizes below is a field in DB with comma separated values eg:(large,small,medium)
$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>
asch
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Mar 18, 2012 9:48 am


Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.