html 中的复选框

Checkbox in html

我有这组复选框

提交后..我想在另一页中回显选中的复选框(pay.php)

例如:如果我在 pay.php 页面中选择了 park and wash .. 我想回显 park and wash 但我只得到 wash(最后选择的复选框)所以如何打印所有选择的复选框??

 <form name="input" action="pay.php" method="post">
Services:
         <br/> 

        <input type="checkbox" name="service" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service" value="wash">Wash <br/>
        <input type="checkbox" name="service" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

在PAY.php:

<?php


//$servicetext=$_POST["service"];
// echo $servicetext;
     ////THE ARRAY PART////

 echo "<table border='0'>
    <tr>
    <th> //PRINT THE ARRAY HERE </th>
    <th>  </th>
    <th>  </th>
    <th>  </th>
    <th>  </th>
<tr/>";

?>

将复选框值添加到数组中,然后通过此数组访问所有发布的值:

HTML:

<form name="input" action="pay.php" method="post">
Services:
         <br/> 

        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

PHP:

现在所有发布的值都存储在 $service[] 数组中。

<?php
$servicetext=$_POST["service"];
var_dump($servicetext); // show all the posted values (content of posted array)
?>

您需要将复选框名称设为数组,如下所示service[]

修改您的表格如下:

<form name="input" action="pay.php" method="post">
Services:
         <br/> 
        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

由于复选框有多个,您需要为该输入类型的相同名称创建一个名称数组。

 <form name="input" action="pay.php" method="post">
    Services:
             <br/> 

        <input type="checkbox" name="service[]" value="park" checked>Park Only <br/> 
        <input type="checkbox" name="service[]" value="wash">Wash <br/>
        <input type="checkbox" name="service[]" value="Check tires">Check Tires <br/>
        <input type="checkbox" name="service[]" value="Fill oil">Fill Oil <br/>
        <input type="checkbox" name="service[]" value="Check brakes">Check Brakes <br/>

        <input type="submit" value="Go to Paying" />
    </form>

在 PAY.PHP 中,您可以访问以下格式的每个复选框值

<?php
if(!empty($_POST['service'])) {
    foreach($_POST['service'] as $service) {
          echo  $service;
         //rest of your code
    }
}

已编辑

在PAY.php

<?php
if(!empty($_POST['service'])) {

$i = 0;
$selArr = array(); //i took an array that will store all these check box values
    foreach($_POST['service'] as $service) {
         $selArr[$i] = $service;
         $i++;
    }
}

已编辑2

<?php
if(!empty($_POST['service'])) {

$i = 0;
$selArr = array(); //i took an array that will store all these check box values
?>
<table>
        <?php 
         foreach($_POST['service'] as $key=>$service) {
         ?>

         <tr><td><?php echo $key; ?></td><td><?php echo $service; ?></td></tr>
         <?php
        }
            ?>
</table>

    <?php
}

希望对您有所帮助。