【问题标题】:Checkbox in htmlhtml中的复选框
【发布时间】:2015-05-15 09:06:35
【问题描述】:

我有这组复选框

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

例如:如果我在 pay.php 页面中选择了 park andwash .. 我想回显 park andwash 但我只得到了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>

IN 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/>";

?>

【问题讨论】:

标签: php html checkbox


【解决方案1】:

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

 <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
}

希望对你有帮助。

【讨论】:

  • @Sourbah 你有办法保存然后在一个数组中然后打印那个数组吗?看来你的方式可能行得通
  • @CodeX,我在编辑部分添加了用于存储在数组中的代码。
  • @Sourbah 只是现在你有一个数组,但我不能把要输出的函数放在表单中..请检查上面的编辑以更好地理解..顺便谢谢!
  • @CodeX,我实际上并没有理解您要实现什么,如果您正在谈论将该数组作为参数传递给函数,那么在 foreach() 循环完成后,您可以传递它数组作为普通参数。
  • 我有一个服务页面和支付页面.. 在服务中:您从复选框中选择并在文本框中输入其他数据,然后单击提交按钮转到 pay.php ... 在支付中:你会得到一个你输入的数据表(比如收据)然后你点击支付....所以现在我们将复选框作为一个数组..我想将它们打印在表格的位置@Sourabh
【解决方案2】:

您需要将复选框名称设置为数组,如下所示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>

【讨论】:

  • 它与单词数组相呼应.. @Jenis Patel
  • 试着像这样检查数组 => print_r($_POST["service"]);
【解决方案3】:

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

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)
?>

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    相关资源
    最近更新 更多