我正在尝试编写一个PHP脚本,该脚本将计算检查复选框的总数,并显示客户的姓名,卡类型和卡号。这是通过提交按钮完成的。我在编写脚本的复选框部分时遇到问题,我不确定我是否正确执行此操作。有没有人有这种问题的简单解决方案?
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<?php
if(isset($_POST['submit'])) {
if (isset($_POST['bulb1'])) {
echo "Your total is $9.56"; }
if (isset($_POST['bulb2'])) {
echo "Your total is $34.32"; }
if (isset($_POST['bulb3'])) {
echo "Your total is $15.80"; }
if (isset($_POST['bulb4'])) {
echo "Your total is $59.92"; }
$cName = $_POST['name'];
$cardType = $_POST['list'];
$cardNum = $_POST['number'];
echo $cName;
echo $cardType;
echo $cardNum;
$total_count = count($_POST['bulb']);
$total_amount = 0;
if(!empty($_POST['bulb'])){
foreach($_POST['bulb'] as $bulb){
$total_amount += $bulb;
}
}
echo 'total amount is ' . $total_amount;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assignment 1: #4</title>
</head>
<body>
<div align="center">
<header>
<h1>CS310: Assignment 5: #2</h1><hr/>
</header>
<p>Please input your name in the box below!</p>
<form>
<input type="text" name="name" size="40"/><hr>
<h3>Four 100-watt light bulbs for $ 2.39</h3>
<input type="checkbox" name="bulb[]" value="9.56" id="Lightbulb1"/><label for="Lightbulb1">Check box for Lightbulb Option Number 1!</label>
<h3>Eight 100-watt light bulbs for $ 4.29</h3>
<input type="checkbox" name="bulb[]" value="34.32" id="Lightbulb2"/><label for="Lightbulb1">Check box for Lightbulb Option Number 2!</label>
<h3>Four 100-watt, long-life light bulbs for $ 3.95</h3>
<input type="checkbox" name="bulb[]" value="15.80" id="Lightbulb3"/><label for="Lightbulb1">Check box for Lightbulb Option Number 3!</label>
<h3>Eight 100-watt, long-life light bulbs for $ 7.49</h3>
<input type="checkbox" name="bulb[]" value="59.92" id="Lightbulb4"/><label for="Lightbulb1">Check box for Lightbulb Option Number 4!</label><hr>
<input type="radio" name="list" value="V" id="Visa"/>Visa
<input type="radio" name="list" value="M" id="MasterCard"/>MasterCard
<input type="radio" name="list" value="A" id="American Express"/>American Express
<input type="radio" name="list" value="D" id="Discover"/>Discover
<hr><br>
Enter your credit card number here:<input type="text" name="number" id="creditCardNumber" />
<br><br>
<input type="submit" name="submit" value="Submit" />
<br>
</form>
<hr><br>© Created by: Jamie McGraw, 6/16/15,
</div>
</body>
</html>
答案 0 :(得分:2)
您可以在html中进行一些更改
<h3>Four 100-watt light bulbs for $ 2.39</h3>
<input type="checkbox" name="bulb[]" value="9.56" id="Lightbulb1"/><label for="Lightbulb1">Check box for Lightbulb Option Number 1!</label>
<h3>Eight 100-watt light bulbs for $ 4.29</h3>
<input type="checkbox" name="bulb[]" value="34.32" id="Lightbulb2"/><label for="Lightbulb1">Check box for Lightbulb Option Number 2!</label>
<h3>Four 100-watt, long-life light bulbs for $ 3.95</h3>
<input type="checkbox" name="bulb[]" value="15.80" id="Lightbulb3"/><label for="Lightbulb1">Check box for Lightbulb Option Number 3!</label>
<h3>Eight 100-watt, long-life light bulbs for $ 7.49</h3>
<input type="checkbox" name="bulb[]" value="59.92" id="Lightbulb4"/><label for="Lightbulb1">Check box for Lightbulb Option Number 4!</label><hr>
创建输入复选框以将数组作为灯泡[]
现在在你的php脚本
// for counting total no of checkbox checked
$total_count = count($_POST['bulb']);
// for total amount initialize it with zero
$total_amount = 0;
if(!empty($_POST['bulb'])){
foreach($_POST['bulb'] as $bulb){
$total_amount += $bulb;
// getting total amount by adding it to every new value
}
}
echo 'total amount = ' . $total_amount;
echo "<br>";
echo 'Card Type = ' . $_POST["list"];
echo "<br>";
echo 'Card Number = ' . $_POST["number"];
答案 1 :(得分:1)
需要在“发布或获取”的表单中指定方法,对于您来说,“post method”
使用
之类的表格<form name="item" method="post">