根据 php 和 jquery 的下拉选择更改价格变量

Change price variable based on a dropdown selection with php and jquery

问题:如何从下拉菜单中获取所选价格并使用它来代替数据库中存储的当前产品的原始价格?

我的方法: 我已经能够使用所选价格更新标签,但我仍然需要覆盖产品的当前价格。我现在很无能。

附加信息:数据库中保存的当前价格是 $price 需要被 $prijshalf[=35 覆盖=] 或 $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;

我看不到您将商品添加到购物车的过程如何,所以我无法告诉您如何操作。但是您可以尝试将下拉列表移动到 <form> 标记中,稍后使用 php 获得值,如果它是 prijshalfprijsheel 则进行数学计算。与 js 相同,如果您不希望下拉菜单进入 <form>,请将下拉菜单的选定值放入 <form> 的隐藏输入中,但我不明白为什么它不应该,将它发送到服务器并在那里进行数学计算。一切由你决定。

始终使用 js 只是为了显示信息,出于安全原因,始终在服务器中使用 php 进行计算和验证。

编码愉快