0

因此,我的表单效果很好,但我一直在尝试为其添加定价。基本上html看起来像这样:

HTML:

<div id="rightDriver">
                    <!-- Start Form -->

                    <!-- This is the club photos -->
                    <div class="clubPhotos">
                        <div class="club">
                            <img src="clubs/img/praxispi.png" class="clubPhoto" />
                            <span class="clubTitle">Praxis PI</span>
                            <span class="clubSub">Low Launch 9.5</span>
                            <input type="checkbox" id="clubCheckbox4" class="clubCheckbox" name="DriverType" value="Praxis PI Low Launch 9.5" />

                        </div>

                        <div class="club">
                            <img src="clubs/img/praxispi.png" class="clubPhoto" />
                            <span class="clubTitle">Praxis PI</span>
                            <span class="clubSub">Mid-Low Launch 12.5</span>
                            <input type="checkbox" id="clubCheckbox1" class="clubCheckbox" name="DriverType" value="Praxis PI Mid-Low Launch 12.5" />
                        </div>
                        <div class="club">
                            <img src="clubs/img/praxispi.png" class="clubPhoto" />
                            <span class="clubTitle">Praxis PI</span>
                            <span class="clubSub">Mid-High Launch 15.5</span>
                            <input type="checkbox" id="clubCheckbox2" class="clubCheckbox" name="DriverType" value="Praxis PI Mid-High Launch 15.5" />
                        </div>
                        <div class="club last">
                            <img src="clubs/img/praxispi.png" class="clubPhoto" />
                            <span class="clubTitle">Praxis PI</span>
                            <span class="clubSub">High Launch 18.5</span>
                            <input type="checkbox" id="clubSelect" class="clubCheckbox" name="DriverType" value="Praxis PI High Launch 18.5" />
                        </div>

                        <div class="clear"></div>
                    </div>

                    <!-- This is the driver order info -->
                    <div class="clubForm">
                        <span class="heading">Club Specs</span>
                        <select name="DriverLength" class="select">
                          <option value="" selected="selected">Driver Length</option>
                          <option value="47.5">47.5</option>
                          <option value="47">47</option>
                          <option value="46.5">46.5</option>
                          <option value="46">46</option>
                          <option value="45.5">45.5</option>
                          <option value="45">45</option>
                          <option value="44.5">44.5</option>
                          <option value="44">44</option>
                          <option value="43.5">43.5</option>
                          <option value="43">43</option>
                          <option value="42.5">42.5</option>
                          <option value="42">42</option>
                        </select>

                        <select name="DriverShaft" class="select">
                          <option value="" selected="selected">Select Shaft</option>
                           <option value="LE- light weight graphite weakest flex">LE- light weight graphite weakest flex</option>
                          <option value="LW- Light weight graphite weak flex">LW- Light weight graphite weak flex</option>
                          <option value="LM- Light weight graphite medium flex">LM- Light weight graphite medium flex</option>
                          <option value="LR- Light weight graphite regular flex">LR- Light weight graphite regular flex</option>
                          <option value="GR- Medium weight graphite regular flex">GR- Medium weight graphite regular flex</option>
                          <option value="SS- Light weight graphite stiff flex">SS- Light weight graphite stiff flex (low torque)</option>
                          <option value="TS- Medium weight graphite stiff flex">TS- Medium weight graphite stiff flex</option>
                          <option value="SX- Light weight graphite X-stiff">SX- Light weight graphite X-stiff</option>
                          <option value="KS- Light weight steel stiff flex">KS- Light weight steel stiff flex</option>
                          <option value="KR- Light weight steel regular flex">KR- Light weight steel regular flex</option>
                          <option value="DX- Heavy weight steel extra stiff">DX- Heavy weight steel extra stiff</option>
                          <option value="DS- Heavy weight steel stiff flex">DS- Heavy weight steel stiff flex</option>
                        </select>

                        <select name="DriverPured" class="select">
                          <option value="" selected="selected">PURE'd</option>
                          <option value="high">High</option>
                          <option value="low">Low</option>
                        </select>
                    </div>

                </div>

评论看起来像这样:

echo "<h3> Driver</h3>";
                echo "<hr>";
                    echo (!empty($_REQUEST['DriverType'])) ? "<div class='reviewItem'><span class='reviewTitle'>Driver:</span>{$_REQUEST['DriverType']}</div>" : "";
                    echo (!empty($_REQUEST['DriverShaft'])) ? "<div class='reviewItem'><span class='reviewTitle'>Shaft:</span>{$_REQUEST['DriverShaft']}</div>" : "";
                    echo (!empty($_REQUEST['DriverLength'])) ? "<div class='reviewItem'><span class='reviewTitle'>Length:</span>{$_REQUEST['DriverLength']}</div>" : "";
                    echo (!empty($_REQUEST['DriverPured'])) ? "<div class='reviewItem'><span class='reviewTitle'>Pured:</span>{$_REQUEST['DriverPured']}</div>" : "";

我想要发生的是将价格添加到“轴”并将其乘以标记的复选框数量。我不知道从哪里开始。我知道我可以将价格添加到轴“价值”,但是如果我用“价值”的数字替换它,我将如何处理轴的名称我想在 PHP 中创建一个数组,但任何帮助都会很棒。提前谢谢。

4

2 回答 2

0

完成此操作的最简单方法是为您的轴价格创建一个查找表。

<?php
$shaftPrices = array(
    "LE- light weight graphite weakest flex"=>10,
    "LW- Light weight graphite weak flex"=>10,
    "LM- Light weight graphite medium flex"=>10,
    "LR- Light weight graphite regular flex"=>10,
    "GR- Medium weight graphite regular flex"=>10,
    "SS- Light weight graphite stiff flex"=>10,
    "TS- Medium weight graphite stiff flex"=>10,
    "SX- Light weight graphite X-stiff"=>10,
    "KS- Light weight steel stiff flex"=>10,
    "KR- Light weight steel regular flex"=>10,
    "DX- Heavy weight steel extra stiff"=>10,
    "DS- Heavy weight steel stiff flex"=>10);

然后要获得所选轴的价格,您可以

$shaftPrice = $shaftPrices[$_POST["DriverShaft"]];
于 2013-07-02T21:48:35.457 回答
0

我不太清楚您是想在表单中还是在服务器上执行此操作。如果在服务器上,请参阅@Orangepill 的答案。当然,你也可以两者都做。

要在客户端上执行此操作,我会开始考虑您的轴可能来自数据库,并且每个轴都有一个 ID。

所以你不需要一个完整的值,你可以用一个 ID 来做:

<option value="17">LE- light weight graphite weakest flex</option>

在这里,该特定轴的值应该是 17。

然后,如果您不使用 Backbone 或 jQuery 等库,您可以在选项中添加值并在纯 Javascript 中检索它。不是很干净(它实际上是 Jaywalking 反模式的一个实例),但它有效:

<option value="17:45.99">LE- light weight graphite weakest flex (US$ 45,99)</option>

现在在 Javascript 中,您可以枚举各种复选框并计算它们,您还可以检索当前轴:

<select name="DriverShaft" class="select" id="DriverShaftId">

...

combo = document.getElementById('DriverShaftId');

value = combo.options[combo.selectedIndex].value;

// "value" is now "17:45.99"

vars  = value.split(':');

// vars[0] is "17", vars[1] is "45.99"

cost = numOfCheckedBoxes * vars[1];

现在您可以将值放入一个新字段:

document.getElementById('total').innerHTML = cost;

“总”元素可能是

Your club will cost US$ <span id="total">...</span>

一个非常简单的小提琴(注意复选框未绑定;您可能希望它们绑定,以便在切换复选框时更新价格)在这里

但是,我建议您查看jQuery Javascript 库。

总结一下:当用户提交表单时,您(在 PHP 端)收到 Orangepill 数组的索引,或数据库表的 ID。从中,您可以再次检索所有轴信息(包括价格),验证数据并计算总数。

于 2013-07-02T21:52:13.097 回答