3.编写一段创建一个表单的HTML 代码,表单包含以下内容: 1)一个用于输入用户名的文本小组件 2)4 个复选框,各项内容为: a)4 个25 瓦的灯泡,金额为$2.39 b)8 个25 瓦的灯泡,金额为$4.29 c)4 个25 瓦的长寿命灯泡,金额为$3.95 d)8 个25 瓦的长寿命灯泡,金额为$7.49 3)由三个单选按钮组成的一个按钮集,标签为: a)Visa b)MasterCard c)Discover 再编写一个PHP 脚本,计算订购的灯泡在加了6.2%的销售税之后的总金额。程序必须返回给顾客一个表格,表格中准确地列出订购的商品。
时间: 2024-02-22 12:58:43 浏览: 57
以下是创建表单的 HTML 代码:
```html
<form method="post" action="calculate.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label>Light bulbs:</label><br>
<input type="checkbox" id="bulb1" name="bulbs[]" value="2.39">
<label for="bulb1">4 x 25W bulbs ($2.39)</label><br>
<input type="checkbox" id="bulb2" name="bulbs[]" value="4.29">
<label for="bulb2">8 x 25W bulbs ($4.29)</label><br>
<input type="checkbox" id="bulb3" name="bulbs[]" value="3.95">
<label for="bulb3">4 x 25W long-life bulbs ($3.95)</label><br>
<input type="checkbox" id="bulb4" name="bulbs[]" value="7.49">
<label for="bulb4">8 x 25W long-life bulbs ($7.49)</label><br><br>
<label>Payment method:</label><br>
<input type="radio" id="visa" name="payment" value="Visa">
<label for="visa">Visa</label><br>
<input type="radio" id="mastercard" name="payment" value="MasterCard">
<label for="mastercard">MasterCard</label><br>
<input type="radio" id="discover" name="payment" value="Discover">
<label for="discover">Discover</label><br><br>
<input type="submit" value="Submit">
</form>
```
以下是 PHP 脚本代码:
```php
<?php
$username = $_POST["username"];
$bulbs = $_POST["bulbs"];
$payment = $_POST["payment"];
$total = 0;
foreach ($bulbs as $bulb) {
$total += $bulb;
}
$total_with_tax = $total * 1.062;
echo "<h2>Order Summary for $username</h2>";
echo "<table>";
echo "<tr><th>Item</th><th>Price</th></tr>";
foreach ($bulbs as $bulb) {
echo "<tr><td>Light bulbs</td><td>$".number_format((float)$bulb, 2, '.', '')."</td></tr>";
}
echo "<tr><td>Sales tax (6.2%)</td><td>$".number_format((float)($total_with_tax - $total), 2, '.', '')."</td></tr>";
echo "<tr><th>Total</th><th>$".number_format((float)$total_with_tax, 2, '.', '')."</th></tr>";
echo "</table>";
?>
```
该脚本会计算订购的灯泡在加了 6.2% 的销售税之后的总金额,并以表格形式返回给顾客准确地列出订购的商品。
阅读全文