title: How do you store the selected option from a dynamic drop down list
body: <p>EDIT:</p>
  
  <p>So, I have the color transferring to my cart page, but It is selecting the last option in my drop down when I try to add it to cart. I've been looking at ways to implement selected='selected', but it's a pain in this while loop.</p>
  
  <p>My current products page:</p>
  
  <pre><code>        &lt;?php
  include("include/db.php");
  include("include/productfunctions.php");
  
   if($_REQUEST['command'] == 'add' &amp;&amp; $_REQUEST['productid'] &gt; 0 &amp;&amp;   $_REQUEST['Color'] != ' ')
   {
      $product_id = $_REQUEST['productid'];
      $color = $_REQUEST['Color'];
      add_to_cart($product_id, 1, $color);
      header("Location:cart.php");
      exit();
   }
  ?&gt;
  
  &lt;script type="text/javascript"&gt;
  //This js is necessary to determine which button on the 
  // form the user clicked.
  
  function addtocart(prod_id, color_ID)
  {
  document.productform.productid.value = prod_id;
  document.productform.Color.value = color_ID;
  document.productform.command.value = 'add';
  document.productform.submit();
  }
  &lt;/script&gt;
  
  &lt;form name="productform" action=""&gt;
  &lt;input type="hidden" name="Color" /&gt;
  &lt;input type="hidden" name="productid" /&gt;
  &lt;input type="hidden" name="command" /&gt;
  &lt;/form&gt;
  
  &lt;table id="product_table"&gt;
  &lt;?php
  
  $query = "SELECT * FROM product";
  $result = mysqli_query($dbc, $query) or die("Error querying database");
  while($row = mysqli_fetch_array($result))
  {
      $p_ID = $row['Prod_ID'];
  
      echo '
      &lt;div class="img"&gt;
      &lt;strong&gt;'.$row['name'].'&lt;/strong&gt;
  
      &lt;br&gt;&lt;img src="' . $row['image'] . '" /&gt;&lt;/br&gt;
  
      &lt;div class="desc"&gt; 
      Description: ' . $row['description'] . '&lt;br&gt;        
      &lt;br&gt;Price: $ ' . $row['price'] . '&lt;/br&gt;
      &lt;br&gt;
      &lt;/div&gt;';
      $query1 = "SELECT * FROM color c JOIN inventory i ON ( c.Color_ID =  i.Color_ID ) JOIN product p ON ( p.Prod_ID = i.Prod_ID ) WHERE p.Prod_ID = $p_ID"; 
      $result1 = mysqli_query($dbc, $query1) or die("Error querying database");
  
      Print "&lt;p&gt;Decorative Color:\n";
      Print "&lt;select name=\"Color_ID\"&gt;\n";
  
      while($row = mysqli_fetch_array($result1))
      {
          $color_ID = $row['Color_ID'];
  
          Print "&lt;option value=".$row['Color_ID']."&gt; " . $row['Color'] . "\n     &lt;/option&gt;";
  
      }
  
      Print "&lt;/select&gt;\n";
      Print "&lt;/p&gt;\n";
  
      echo '&lt;div class="checkout"&gt;
      &lt;input type="button" value="Add to Cart" onclick="addtocart('. $p_ID .    ','. $color_ID . ')" /&gt;&lt;/br&gt;
      &lt;/div&gt;
      &lt;/div&gt;';
  
  } 
  
  ?&gt;
  
  &lt;/table&gt;
  </code></pre>
  
  <p>function to add to cart:</p>
  
  <pre><code>    function add_to_cart($product_id, $quantity, $color)
  {
  if($product_id &lt; 1 || $quantity &lt; 1)
  {
      return;
  }
  
  if(is_array($_SESSION['cart']))
  {
      $exists_results = product_exists($product_id);
      $exists = $exists_results[0];
      $position = $exists_results[1];
  
      if($exists)
      {
          $new_quantity = intval($_SESSION['cart'][$position]['quantity']);
          $new_quantity++;
          $_SESSION['cart'][$position]['quantity'] = $new_quantity;
      }
      else
      {
          $max = count($_SESSION['cart']);
          $_SESSION['cart'][$max]['productid'] = $product_id;
          $_SESSION['cart'][$max]['quantity'] = $quantity;
          $_SESSION['cart'][$max]['Color'] = $color;
      }
  
  }
  
  else
  {
      $_SESSION['cart'] = array();
      $_SESSION['cart'][0]['productid'] = $product_id;
      $_SESSION['cart'][0]['quantity'] = $quantity;
      $_SESSION['cart'][0]['Color'] = $color;
  }
  }
  
  
  
   function product_exists($product_id)
  {
  $product_id = intval($product_id);
  $max = count($_SESSION['cart']);
  $flag = 0;
  
  for($i=0; $i &lt; $max; $i++)
  {
          if($product_id == $_SESSION['cart'][$i]['productid'])
          {
              $flag = 1;
              break;
          }
  }
  return array ($flag, $i);
  }
  
  ?&gt;
  </code></pre>
  
  <p>The cart:</p>
  
  <pre><code>&lt;?php
  if(count($_SESSION['cart']))
  {
      echo '&lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;
      &lt;th&gt;Price&lt;/th&gt;
      &lt;th&gt;Decorative Color&lt;/th&gt;
      &lt;th&gt;Qty&lt;/th&gt;
      &lt;th&gt;Amount&lt;/th&gt;
      &lt;th&gt;Options&lt;/th&gt;
      &lt;/tr&gt;';
  
      $max = count($_SESSION['cart']);
      for($i = 0; $i &lt; $max; $i++)
      {
          $product_id = $_SESSION['cart'][$i]['productid'];
          $quantity = $_SESSION['cart'][$i]['quantity'];
          $color = $_SESSION['cart'][$i]['Color'];
          $product_name = get_product_name($dbc, $product_id);
          $product_price = get_price($dbc, $product_id);
          $product_color = get_color($dbc, $color);
  
          if($quantity == 0)
          {
              continue;
          }
  
          echo '&lt;tr&gt;
          &lt;td&gt;' . $product_name . '&lt;/td&gt;
          &lt;td&gt;&amp;#36; ' . $product_price . '&lt;/td&gt;
          &lt;td&gt;' . $product_color . '&lt;/td&gt;
          &lt;td&gt;&lt;input type="text" name="product' . $product_id . '" value="' .
                          $quantity . '" maxlength="4"   size="2" /&gt;&lt;/td&gt;
          &lt;td&gt;&amp;#36; ' . $product_price*$quantity . '&lt;/td&gt;
          &lt;td&gt;&lt;a href="javascript:del(' . $product_id . ')"&gt;Remove Item&lt;/a&gt;   &lt;/td&gt;
          &lt;/tr&gt;';         
      }
  
      echo '&lt;tr&gt;
      &lt;td colspan="2"&gt;&lt;strong&gt;Order Total: &amp;#36;' . get_order_total($dbc) .   '&lt;/strong&gt;&lt;/td&gt;
      &lt;td&gt;&lt;/td&gt;
      &lt;td colspan="3" id="cart_buttons"&gt;
      &lt;input type="submit" value="Clear Cart" onclick="clear_cart()"&gt;
      &lt;input type="submit" value="Update Cart" onclick="update_cart()"&gt;
      &lt;input type="submit" value="Complete Order" onclick="complete_order()"&gt;
      &lt;/td&gt;
      &lt;/tr&gt;';
  }
  else
  {
      echo '&lt;tr&gt;&lt;td&gt;There are no items in your shopping cart.&lt;/td&gt;&lt;/tr&gt;';
  }
  ?&gt;
  </code></pre>
  
  <p><strong>EDIT 2:</strong></p>
  
  <pre><code>function addtocart(prod_id)
  {
  document.productform.productid.value = prod_id;
  document.productform.Color.value = $("select[name='color_' + prod_id] option:selected").val();
  document.productform.command.value = 'add';
  document.productform.submit();
  }
  </code></pre>
  
  <p>Other Edit:</p>
  
  <pre><code>Print "&lt;select name=\"Color_" . $p_ID . "\"&gt;";
  </code></pre>