问题:如何从下拉菜单中获取所选价格并使用它而不是存储在数据库中的当前产品的原始价格?
我的方法:我已经能够以所选价格更新标签,但我仍需要覆盖产品的当前价格。我现在很无能为力。
其他信息:数据库中保存的当前价格是 $ price ,需要被 $ prijshalf 或 $ prijsheel 也都存储在数据库中。
用于更新标签的JQuery代码:
<script type="text/javascript">
$(document).ready(function() {
$('.dropdown').change(function() {
var price = 0;
$('.dropdown').each(function() {
if($(this).val() != 0) {
price = parseFloat($(this).val());
}
});
$('#costLabel').text('€ ' + price.toFixed(2));
});
});
</script>
此代码块获取所选下拉菜单项的值,并将其放在标签#costLabel
中用于呈现产品的PHP代码:
$product_list = "";
while($row = mysql_fetch_array($results)){
$id = $row["id"];
$product_name = substr($row["product_name"],0,25);
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$beschrijving = substr($row["details"],0,25);
$afbeelding = '<img src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="50" height="50" />';
$dropdown = $row["dropdown"];
$dropdown2 = $row["dropdown2"];
$prijshalf = $row["prijshalf"];
$prijsheel = $row["prijsheel"];
if($dropdown != "" || $dropdown2 != ""){
$dropdownshow = "<select name='dropdown' id='dropdown' class='dropdown'>
<option name='prijshalf' value='$prijshalf'>$dropdown</option>
<option name='prijsheel' value='$prijsheel'>$dropdown2</option>
</select>";
}
else{
$dropdownshow = "";
}
$product_list .= "<div class='produkt'>
<div class='hardlopers'>
$afbeelding
<div class='subinfo'>
<span class='prijs-info'>Prijs:</span><span class='prijs'><label id='costLabel' name='costLabel'>$price</label> </span>
<span class='productnaam'>$product_name</span>
<span class='dropdown'>
$dropdownshow
</span>
<form id='form1' name='form1' method='post' action='homepage.php#redirect'>
<input type='hidden' name='pid' id='pid' value='$id' />
<input type='submit' name='button' id='button' value='' />
</form>
<span class='info'></span>
</div>
</div>
</div>
";
}
// Prints out the products to screen
echo $product_list;
答案 0 :(得分:1)
我无法看到您将商品添加到购物车的过程如何,所以我无法告诉您如何操作。但是您可以尝试将下拉列表移动到<form>
标记中,稍后使用php获取值,如果是prijshalf
或prijsheel
则进行数学运算。与js
相同,如果您不希望下拉列入<form>
,则将下拉列表的选定值放在<form>
中的隐藏输入中,但我不明白为什么它不应该,发送到服务器并在那里做数学。一切都取决于你。
始终仅使用js
来显示信息,出于安全原因,请始终使用服务器中的php
进行计算和验证。
快乐编码