将所选产品添加到php中的表中

时间:2014-07-12 12:00:27

标签: php html

我在页面中有3个选择表单和一个addToCart按钮。我可以从db获取数据以显示选择表单中的产品,没有问题。当用户单击addToCart按钮时,我需要从选择表单中获取选定的值并将它们写入同一页面中的表。这是选择表格和按钮。你可以帮我在php中写一个函数来做这件事。

 <select class="form-control" id="chooseItem">
  <option disabled selected>Buradan Seç...</option>

<?php

  $sql="SELECT * FROM products ORDER BY productName ASC";
  $query = mysql_query($sql);
  $optionBeverage= '';

  while ($row = mysql_fetch_array($query)) {
    $optionBeverage .= '<option value =  "'.$row['productName'].'">'.$row['productName'].'</option>';
  }

?>
<?php echo $optionBeverage; ?>

 </select> 
 <h3>Sandviçler & Hamur İşleri</h3>
 <select class="form-control" id="chooseItem">
  <option disabled selected>Buradan Seç...</option>

<?php

  $sql="SELECT * FROM products ORDER BY productName ASC";
  $query = mysql_query($sql);
  $optionSandwich= '';

  while ($row = mysql_fetch_array($query)) {
   $optionSandwich .= '<option value = "'.$row['productName'].'">'.$row['productName'].'</option>';
  }

?>
<?php echo $optionSandwich; ?>

 </select>
 <h3>Tatlılar</h3>
 <select class="form-control" id="chooseItem">
  <option disabled selected>Buradan Seç...</option>

<?php

  $sql="SELECT * FROM products ORDER BY productName ASC";
  $query = mysql_query($sql);
  $optionDesert= '';

  while ($row = mysql_fetch_array($query)) {
   $optionDesert .= '<option value = "'.$row['productName'].'">'.$row['productName'].'</option>';
  }

?>
<?php echo $optionDesert; ?>

 </select>
 <p>
  <br>
  <button type="button" class="btn btn-primary btn-lg"  onclick="xx">Sepete Ekle</button>
 </p>
</div>

1 个答案:

答案 0 :(得分:0)

你需要在这里使用javascript来获取select的值。

$('#addtocart').on('click',function(){ //on add to cart button clicked

   selectedOption=$( "#myselect option:selected" ).text(); get value myselect's value

   //write selectedOption anywhere you want
   //Example: $('#myDiv').html(selectedOption);   

});

让每个人选择id(chooseItem)唯一。

如果您需要将其写入数据库,请使用ajax。