CptTritium
CptTritium

Reputation: 23

Passing Multiple Values to a PHP Shopping Cart

First of all, this is my first PHP store, so if I seem a bit daft, I apologize.

Short form: How can I pass a set of values to $_SESSION variables for a PHP shopping cart, without using a separate form for each item on a page?

Here's the situation. I'm trying to create a store. The products on each page will be called from a MySQL database, and organized by cost. This is all working perfectly.

The problem I'm running into involves the item options. Some of these items, like ballcaps, T-shirts, and other assorted goods, will have multiple sizes/colors. The way I have this organized now is that each entry in the MySQL table that matches the criteria is output into its own form, with options for size and color, if applicable. The issue I'm having with this is that while I can apparently post these to another page, I cannot seem to assign them to a variable.

I would love to do away with the forms entirely, and use a hyperlink like this: Add to cart but I'm not sure it's an option, ad I'd need to pass the values of the size and color on some items.

I know this has been done before, and often, but I can't seem to get Google to help me out here. Any advice/tips/tricks would be greatly appreciated.

Below is my current code, using the multiple forms method I thought would be good when I started:

<?php
while($row = mysql_fetch_array($data)){
  echo "<form method='post' action='addcart.php'>";
  echo "<tr><td><img width='100' width='100' src='itemImages/" . $row['itemIMG'] . "'/></td>
  <td>" . $row['itemNAME'] . "</td>
  <td><p id='description'>" . $row['itemDESC'] . "</p></td> <td>";
  //Color dropdown population happens here
  if($row["itemCOL1"] != ' '){
    echo "<select name=colors>";
    $finished = 'false';
    $i = '0';
    $colrow = mysql_fetch_array($coldata);
    while($finished != 'true'){
       if($colrow[$i] != ' '){  
         echo "<option value=" . $colrow[$i] . ">" . $colrow[$i] . "</option>";
         $i++;
    } else {
      echo "</select>";
      $finished='true';
    }
   }
  }
//Size option dropdown handled here
if($row['itemHSIZ'] != "N"){
  echo "<select name=prodSizes>";
  echo "<option value=X-Small>X-Small  </option>";
  //Sizes omitted for brevity
  echo "</select>";
  }
echo "<input type='hidden' name='prodName' value='" . $row['itemNAME'] . "' />";
echo "<input type='submit' name='submit' value='Submit' /></form>";
};
//...and above here.
mysql_close($con);
?>

Upvotes: 1

Views: 1744

Answers (2)

jpwdesigns
jpwdesigns

Reputation: 21

There are a couple ways you can go about it both using JavaScript. You can use JavaScript to make ajax calls to a php script using onclick events attached to your add to cart button/link or you could use JavaScript to add parameters to your add to cart link when options are selected.

Does that make sense?

PS don't use id on the p tag like that. You want to keep id values unique.

Upvotes: 0

Tom
Tom

Reputation: 308

I would imagine you can move the form open and form close outside of your while loop to have all the items be in one form, and thus one post:

<?php
  echo "<form method='post' action='addcart.php'>";
while($row = mysql_fetch_array($data)){
  echo "<tr><td><img width='100' width='100' src='itemImages/" . $row['itemIMG'] . "'/></td>
  <td>" . $row['itemNAME'] . "</td>
  <td><p id='description'>" . $row['itemDESC'] . "</p></td> <td>";
  //Color dropdown population happens here
  if($row["itemCOL1"] != ' '){
    echo "<select name=colors>";
    $finished = 'false';
    $i = '0';
    $colrow = mysql_fetch_array($coldata);
    while($finished != 'true'){
       if($colrow[$i] != ' '){  
         echo "<option value=" . $colrow[$i] . ">" . $colrow[$i] . "</option>";
         $i++;
    } else {
      echo "</select>";
      $finished='true';
    }
   }
  }
//Size option dropdown handled here
if($row['itemHSIZ'] != "N"){
  echo "<select name=prodSizes>";
  echo "<option value=X-Small>X-Small  </option>";
  //Sizes omitted for brevity
  echo "</select>";
  }
echo "<input type='hidden' name='prodName' value='" . $row['itemNAME'] . "' />";
};
echo "<input type='submit' name='submit' value='Submit' /></form>";
//...and above here.
mysql_close($con);
?>

Let me know if this isn't what you meant!

Upvotes: 1

Related Questions