如何输出所有复选框值

时间:2015-08-27 22:10:12

标签: php forms validation checkbox output

你好,请帮助我,我有一个问题输出复选框的所有值只输出一个我需要显示所有选中的复选框并输出它们请帮助我这里是代码它只显示一个每当我检查他们所有我需要这个

<html>
    <head>
        <title>Cake Form</title>
        <link rel="stylesheet" href="cakeform.css">
    </head>
    <body>
    <?php

        $nameErr = $addErr = $phoneErr = $scake  = $flavorcake = $fill = "";
$name = $address = $phone =  $rcake = $fillr = $cakeflavor = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
   }

   if (empty($_POST["address"])) {
     $addErr = "Email is required";
   } else {
     $address = test_input($_POST["address"]);
   }

   if (empty($_POST["phone"])) {
     $phoneErr = "Gender is required";
   } else {
     $phone = test_input($_POST["phone"]);
   }
   if(isset($_POST['SelectedCake'])){
       $x=$_POST['SelectedCake'];

   }
   if(isset($_POST['CakeFlavor'])){
       $y=$_POST['CakeFlavor'];

   }
   if(isset($_POST['Filling'])){
       $z=$_POST['Filling'];

   }
   if(empty($x)){
       $scake='Select one Cake';


   }else{

       $rcake= $x;

   }
    if(empty($y) OR $y == 'Flavor'){
       $flavorcake='Select one flavor';


   }else{

       $cakeflavor= $y;

   }
   if(empty($z)){
       $fill='Select at least one Fillings';


   }else{
       foreach($z as $item){
           $fillr=$item;

       }


   }



}
function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}


    ?>
    <div id="wrap">
        <div class="cont_order">
        <fieldset>
        <legend>Make your own Cake</legend>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
        <h4>Select size for the Cake:</h4>
        <input type="radio" name="SelectedCake" value="Round6">Round cake 6" - serves 8 people</br>
        <input type="radio" name="SelectedCake" value="Round8">Round cake 8" - serves 12 people</br>    
        <input type="radio" name="SelectedCake" value="Round10">Round cake 10" - serves 16 people</br>  
        <input type="radio" name="SelectedCake" value="Round12">Round cake 12" - serves 30 people</br>
<span class="error">*<?php echo $scake;?></span>        
        <h4>Select a Cake Flavor: </h4>
        <select name="CakeFlavor">
            <option value="Flavor" selected="selected">Select Flavor</option>
            <option value="Carrot" >Carrot</option>
            <option value="Chocolate" >Chocolate</option>
            <option value="Banana" >Banana</option>
            <option value="Red Velvet" >Red Velvet</option>
            <option value="Strawberry" >Strawberry</option>
            <option value="Vanilla" >Vanilla</option>
            <option value="Combo" >Combo</option>
        </select>
        <span class="error">*<?php echo $flavorcake;?></span>
        <h4>Select Fillings:</h4>
        <label><input type="checkbox" name="Filling[]" value="Cream"/>Cream</label><br>
        <label><input type="checkbox" name="Filling[]" value="Fudge"/>Fudge</label><br>
        <label><input type="checkbox" name="Filling[]" value="Ganache"/>Ganache</label><br>
        <label><input type="checkbox" name="Filling[]" value="Hazelnut"/>Hazelnut</label><br>
        <label><input type="checkbox" name="Filling[]" value="Mousse"/>Mousse</label><br>
        <label><input type="checkbox" name="Filling[]" value="Pudding"/>Pudding</label><br>
        <span class="error">*<?php echo $fill;?></span>
        </fieldset>
        </div>
        <div class="cont_order">
        <fieldset>
        <legend>Contact Details</legend>
        <label for="name">Name</label><br>
        <input type="text" name="name" id="name"><span class="error">*<?php echo $nameErr;?></span>
        <br>
        <label for="address">Address</label><br>
        <input type="text" name="address" id="address"><span class="error">*<?php echo $addErr;?></span>
        <br>
        <label for="phonenumber">Phone Number</label><br>
        <input type="text" name="phone" id="phone"><span class="error">*<?php echo $phoneErr;?></span><br>
        </fieldset>
        <input type="submit" name="submitted" id="submit" value="Submit"/>
        </div>
        </form>
        <div class="cont_order">
        <?php

            echo $name.'<br>';
            echo $address.'<br>';
            echo $phone.'<br>';
            echo $rcake.'<br>';
            echo $cakeflavor.'<br>';
            echo $fillr.'<br>';


        ?>

        </div>
        </div>


        </body>
</html>

2 个答案:

答案 0 :(得分:1)

你可以这样做:

var_dump($_POST['Filling']);

或者只是这个:

<?php
if(!empty($_POST['Filling'])) {
    foreach($_POST['Filling'] as $check) {
            echo $check;     
    }
}
?>

告诉我它是否有效=)

答案 1 :(得分:0)

事实上,您接受填写清单。 在您的代码中,您执行此操作:

foreach($z as $item){
    $fillr=$item;
}

将其替换为:

$fillr = '';
foreach($z as $item){
    $fillr .= '- ' . $item . '<br/>';
}

它应该更好。

评论:

#so let's say $z is an array :

$z = [1,2,3,4];

#then you say, for each element of the array as you will call $item, I do something

foreach($z as $item){
    $fillr=$item; #here you say : for each element of $z (1,2,3,4), I put this value into $fillr.
}

#so let's do the loop together.
# First pass, $item == 1, so $fillr == 1.
# Second pass, $item == 2, so $fillr == 2.
# ...
# Last pass, $item == 4, so $fillr == 4.

# so then when you show $fillr it's the last item of the list (here 4).


# using PHP, .= means concatenate.
# for instance :
$a = "hello ";
$a .= "world";
echo $a;

> hello world


So basically, what I'm doing is :
Having an empty string I'll call $fillr.
For every element of the list,
  Append "the_element plus a <br/>" to $fillr

so then when you output $fillr it looks sexy.


Do you get it bud?