如何将复选框变量携带到另一个页面 - PHP

时间:2013-11-28 13:08:04

标签: php html

我在第1页上有5个复选框,并且希望从数据库的基础上将行提取到第2页。

示例:

if checkbox1 = checked    
    fetch row 1

if checkbox2 = checked    
    fetch row 2

...

等等。

请向我提供一个想法,了解如何在PHP中编写此语法。

2 个答案:

答案 0 :(得分:2)

您可以使用post方法发布所有复选框,然后在操作页面中检查每个复选框。

如果给它们相同的名称,它们将是一个数组,因此您可以使用它们的索引遍历它们。

<form action="page2.php" method="post">
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="checkbox" name="check" />
<input type="submit" />
</form>

在page2.php

$checkArray = $_POST['check'];
$i = 0;
foreach ($checkArray as $checkVal) {
    //some dao you can use with $i
    $i++;
}

答案 1 :(得分:1)

尝试这个想法

<form action="table2.php" method="post">
<input type="checkbox" name='ch1' value="1"> 
<input type="checkbox" name='ch2'  value="2">
<input type="checkbox" name='ch3' value="3">
<input type="checkbox" name='ch4' value="4">
<input type="checkbox" name='ch5'  value="5">
</form>

table2.php

<?php
    for($i=1;$i<6;$i++)
    {

            $fetch=$_POST['ch'.$i];
            if($fetch==$i)
            {
                //fetch row $i
            }

    }

 ?>
相关问题