回显复选框、单选按钮和文本框输入的值


Echoing values of check boxes, radio buttons and text box inputs

我正在尝试编写一个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>&copy; Created by: Jamie McGraw, 6/16/15,
</div>
</body>
</html>

您可以在 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"];

需要以"post or get"的形式指定方法,对您来说它是"发布方法"

使用类似形式的表格

<form name="item" method="post">