2

我有多个复选框,当我检查它们时,想在下一页传递它们的值。我的代码是

<form method="POST">
    <input type="checkbox" name="city" id="city" value="Gold" />
         <input type="checkbox" name="city" id="city" value="Platinum" />
         <input type="checkbox" name="city" id="city" value="Silver" />
    <input type="submit" value="Submit" />
</form>
4

3 回答 3

4

通过将 name 属性定义为数组name="city[]"

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    print_r($_POST);
    /*
    Array
    (
        [city] => Array
            (
                [0] => Gold
                [1] => Platinum
                [2] => Silver
            )

    )
    */
}
?>
<form method="POST">
    <input type="checkbox" name="city[]" value="Gold" />
    <input type="checkbox" name="city[]" value="Platinum" />
    <input type="checkbox" name="city[]" value="Silver" />
    <input type="submit" value="Submit" />
</form>
于 2012-10-29T11:22:43.697 回答
2

尝试这个。

<?php
    include("config.php");
    if($_POST) {
        $city = $_POST['card'];
            header("location:target_page.php?card=".$city);
    }
?>
<form method="POST">
    <input type="checkbox" name="card[]" id="card" value="Gold" />
    <input type="checkbox" name="card[]" id="card" value="Platinum" />
    <input type="checkbox" name="card[]" id="card" value="Silver" />
    <input type="submit" value="Submit" />
</form>
于 2012-10-29T11:22:27.237 回答
1

当两个字段具有相同名称时,这不起作用。

<form method="POST">
    <input type="checkbox" name="city" id="city" value="Gold" />
         <input type="checkbox" name="cities[]" id="city" value="Platinum" />
         <input type="checkbox" name="cities[]" id="city2" value="Silver" />
    <input type="submit" value="Submit" />
</form>

当您编写像 city []这样的字段名时,您会在 PHP 脚本中获得一个数组。

forearch($_POST['cities'] as $city) {
    var_dump($city);
}

ID应该是唯一的。

于 2012-10-29T11:23:16.023 回答