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%的销售税之后的总金额。程序必须返回给顾客一个表格,表格中准确地列出订购的商品。并且php版本为5.6
时间: 2024-02-20 14:57:48 浏览: 73
以下是创建表单的 HTML 代码:
<form method="post" action="order.php">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="bulbs">选择灯泡:</label><br>
<input type="checkbox" id="bulbs1" name="bulbs[]" value="4 bulbs, 25watts, $2.39">
<label for="bulbs1">4个25瓦灯泡,金额为$2.39</label><br>
<input type="checkbox" id="bulbs2" name="bulbs[]" value="8 bulbs, 25watts, $4.29">
<label for="bulbs2">8个25瓦灯泡,金额为$4.29</label><br>
<input type="checkbox" id="bulbs3" name="bulbs[]" value="4 bulbs, 25watts, long-lasting, $3.95">
<label for="bulbs3">4个25瓦长寿命灯泡,金额为$3.95</label><br>
<input type="checkbox" id="bulbs4" name="bulbs[]" value="8 bulbs, 25watts, long-lasting, $7.49">
<label for="bulbs4">8个25瓦长寿命灯泡,金额为$7.49</label><br><br>
<label>支付方式:</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="提交订单">
</form>
以下是计算订单总金额的 PHP 代码:
<?php
// 获取表单提交的数据
$username = $_POST['username'];
$bulbs = $_POST['bulbs'];
$payment = $_POST['payment'];
// 计算总金额
$subtotal = 0;
foreach ($bulbs as $bulb) {
preg_match('/\d+\.\d+/', $bulb, $matches); // 从字符串中提取价格
$price = floatval($matches[0]);
$subtotal += $price;
}
$tax = $subtotal * 0.062;
$total = $subtotal + $tax;
// 输出表格
echo "<table>";
echo "<tr><th>商品</th><th>金额</th></tr>";
foreach ($bulbs as $bulb) {
echo "<tr><td>" . $bulb . "</td><td>$" . number_format(floatval(preg_replace('/[^0-9\.]/', '', $bulb)), 2) . "</td></tr>";
}
echo "<tr><td>小计</td><td>$" . number_format($subtotal, 2) . "</td></tr>";
echo "<tr><td>税</td><td>$" . number_format($tax, 2) . "</td></tr>";
echo "<tr><td>总计</td><td>$" . number_format($total, 2) . "</td></tr>";
echo "</table>";
?>
请注意,此代码仅为演示目的,不适用于生产环境。在实际场景中,需要进行更严格的输入验证和安全措施。
相关推荐


















