使用PHP计算订单的成本

时间:2014-11-14 03:12:58

标签: php forms

所以我对PHP没有任何了解,但我可以通过html,javascript和css中的问题来操纵。我的任务是根据用户输入的项目数量计算订单的成本。所有我真正知道的是设置变量并编写函数,因为它们类似于javascript。到目前为止,表格看起来像这样:

<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html>
<head>
<title>Light bulb Sales</title>
</head>
<body>
<form action="light.php" method="post">
<h2>Welcome to Light of your Life Light bulb Sales</h2>
<table>
<tr>
    <td>Buyer's Name:</td>
    <td><input type="text" name="name" size="30" /></td>
</tr>
<tr>
    <td>Street Address:</td>
    <td><input type="text" name="address" size="30" /></td>
</tr>
<tr>
    <td>City, State, Zip:</td>
    <td><input type="text" name="location" size="30" /></td>
</tr>
</table>
<p />

<table border="border">
<tr>
    <th>Product</th>
    <th>Price</th>
    <th>Quantity</th>
</tr>
<tr>
    <td>100-watt Light Bulbs (4)</td>
    <td>$2.39</td>
    <td align="center"><input type="text" name="fourLight" size="3" /></td>
</tr>
<tr>
    <td>100-watt Light Bulbs (8)</td>
    <td>$4.29</td>
    <td align="center"><input type="text" name="eightLight" size="3" /></td>
</tr>
<tr>
    <td>100-watt long-lie Light Bulbs (4)</td>
    <td>$3.95</td>
    <td align="center"><input type="text" name="fourLong" size="3" /></td>
</tr>
<tr>
        <td>100-watt long-lie Light Bulbs (8)</td>
        <td>$7.49</td>
        <td align="center"><input type="text" name="eightLong" size="3" /></td>
</tr>
</table>
<p />
<input type="submit" value="Submit Order" />
<input type="reset" value="Clear order Form" />
</form>
</body>
</html>

所以我需要php页面做的是发布用户订购的内容以及在表中花费多少。我的PHP页面看起来像这样:

<!DOCTYPE html>
<html>
<head>
<title>Lightbulb sales</title>
</head>
<body>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
$name=$_POST['name'];
$address=$_POST['address'];
$location=$_POST['location'];
$total=;
$tax=.08;
$checkout=;
$quantity=;
$price=;
function computeCost(){

}

print $name;<br />
print $address;<br />
print $location;<br />
?>
</body>
</html>

我不是在找人给我答案,因为我希望学习如何做到这一点,我将不胜感激任何提示或任何例子。

1 个答案:

答案 0 :(得分:2)

这是我为你提出的。我认为把它写出来更容易。

使用众多PHP函数,例如:

旁注:您可能希望使用下拉选项替换用户输入。

PHP:

<!DOCTYPE html>
<html>
<head>
<title>Lightbulb sales</title>
</head>
<body>

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1);

$name = $_POST['name'];
$address = $_POST['address'];
$location = $_POST['location'];

/* ====================== ITEMS ====================== */

/* fourLight */
if(isset($_POST['fourLight'])  && !empty($_POST['fourLight']) && is_numeric($_POST['fourLight']) ){

    $fourLight = $_POST['fourLight'];
    $fourLight_price = "2.39";
    $total_fourLight = $fourLight * $fourLight_price;

echo "Four light: $" . number_format($total_fourLight,2);
echo "<br>";

}
else{ $total_fourLight = 0; }


/* eightLight */
if(isset($_POST['eightLight'])  && !empty($_POST['eightLight']) && is_numeric($_POST['eightLight']) ){

    $eightLight = $_POST['eightLight'];
    $eightLight_price = "4.29";
    $total_eightLight = $eightLight * $eightLight_price;

echo "Eight Light: $" . number_format($total_eightLight,2);
echo "<br>";

}

else{ $total_eightLight = 0; }

/* fourLong */
if(isset($_POST['fourLong']) && !empty($_POST['fourLong']) && is_numeric($_POST['fourLong']) ){

    $fourLong = $_POST['fourLong'];
    $fourLong_price = "3.95";
    $total_fourLong = $fourLong * $fourLong_price;

echo "Four Long: $" . number_format($total_fourLong,2);
echo "<br>";

}

else{ $total_fourLong = 0; }


/* eightLong */
if(isset($_POST['eightLong'])  && !empty($_POST['eightLong']) && is_numeric($_POST['eightLong']) ){

    $eightLong = $_POST['eightLong'];
    $eightLong_price = "7.49";
    $total_eightLong = $eightLong * $eightLong_price;

echo "Eight Long: $" . number_format($total_eightLong,2);
echo "<br>";

}

else{ $total_eightLong = 0; }


/* ====================== TALLY ====================== */

$sub_total = $total_fourLight + $total_eightLight + $total_fourLong + $total_eightLong;

$grand_total_items = number_format($sub_total, 2);

echo "<hr>";
echo "Sub-total: $" . $grand_total_items;
echo "<br>";

$tax= 1.08; // tax is calculated this way, not just .08

$grand_total = $grand_total_items * $tax;

echo "Grand total including 8% tax: $" . number_format($grand_total,2);

echo "<br>";

$checkout = "Checkout text here.";

// Not sure what you want to do with this
function computeCost(){

}

/* ====================== PRINT OTHER ====================== */

print "Name: " . $name . "<br>";
print "Address: " . $address . "<br>";
print "Place: " . $location . "<br>";

print "<hr>";
print $checkout . "<br>";
?>
</body>
</html>

以下是表单的下拉选择版本,它将确保数值。

旁注:如果您决定稍后将其用作“其他”选项,我已使用添加的_other后缀重命名了所有原始输入。这些已被评论<!-- -->

<!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html>
<head>
<title>Light bulb Sales</title>
</head>
<body>
<form action="light.php" method="post">
<h2>Welcome to Light of your Life Light bulb Sales</h2>
<table>
<tr>
    <td>Buyer's Name:</td>
    <td><input type="text" name="name" size="30" /></td>
</tr>
<tr>
    <td>Street Address:</td>
    <td><input type="text" name="address" size="30" /></td>
</tr>
<tr>
    <td>City, State, Zip:</td>
    <td><input type="text" name="location" size="30" /></td>
</tr>
</table>


<table border="4">
<tr>
    <th>Product</th>
    <th>Price</th>
    <th>Quantity</th>
</tr>
<tr>
    <td>100-watt Light Bulbs (4)</td>
    <td>$2.39</td>
    <td align="center">

  <select size="1" name="fourLight">
    <option selected>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>

<!-- commented out input -->
<!--
<input type="text" name="fourLight_other" size="3" />
-->

</td>
</tr>
<tr>
    <td>100-watt Light Bulbs (8)</td>
    <td>$4.29</td>
    <td align="center">

  <select size="1" name="eightLight">
    <option selected>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>

<!-- commented out input -->
<!--
<input type="text" name="eightLight_other" size="3" />
-->

</td>
</tr>
<tr>
    <td>100-watt long-lie Light Bulbs (4)</td>
    <td>$3.95</td>
    <td align="center">

  <select size="1" name="fourLong">
    <option selected>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>

<!-- commented out input -->
<!--
<input type="text" name="fourLong_other" size="3" />
-->

</td>
</tr>
<tr>
        <td>100-watt long-lie Light Bulbs (8)</td>
        <td>$7.49</td>
        <td align="center">

  <select size="1" name="eightLong">
    <option selected>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    <option>10</option>
  </select>

<!-- commented out input -->
<!--
<input type="text" name="eightLong_other" size="3" />
-->

</td>
</tr>
</table>

<input type="submit" value="Submit Order" />
<input type="reset" value="Clear order Form" />
</form>
</body>
</html>

如果您想动态执行下拉菜单的数量,而不是添加选项:

将这些值$i = 0; $i <= 24从0修改为任意数字。

<select name="fourLight">
    <?php for ($i = 0; $i <= 24; $i++) : ?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
    <?php endfor; ?>
</select>

在为其他人更改名称属性的同时复制该代码。