使用关联数组名称构建表单

时间:2015-03-15 10:52:06

标签: php html

我需要构建一个表单来输出以下$ filters数组:

Array
(
    [0] => Array
        (
            [filterType] => hotelStar
            [filterValue] => 3
        )

    [1] => Array
        (
            [filterType] => hotelStar
            [filterValue] => 5
        )

    [2] => Array
        (
            [filterType] => hotelStar
            [filterValue] => 4
        )

    [3] => Array
        (
            [filterType] => resultLimit
            [filterValue] => 2000
        )

)

我的表格是

<form action="output.php" method="get">
<input type="radio" name="filterType[]" value="[hotelStar]3,4,5">
<input type="radio" name="filterType[]" value="[hotelStar]3">
<input type="radio" name="filterType[]" value="[hotelStar]4">
<input type="radio" name="filterType[]" value="[hotelStar]5">
<input type="hidden" name="filterType[]" value="[resultLimit]2000">
<button type="submit">Submit</button>
</form>

我收到的输出是:

Array
(
    [filterType] => Array
        (
            [0] => [hotelStar]3
            [1] => [hotelStar]4
            [2] => [hotelStar]5
            [3] => [resultLimit]2000
        )

)

但输出不是预期的输出。我已经尝试了几乎所有的可能性,但我找不到工作的那个。

1 个答案:

答案 0 :(得分:0)

我的尝试如下:

将表格中的项目命名为正常,例如

<form action="<?$_SERVER['PHP_SELF']?>" method="get">
<input type="radio" name="hotelStar3" value="3">
<input type="radio" name="hotelStar4" value="4">
<input type="radio" name="hotelStar5" value="5">
<input type="hidden" name="resultLimit" value="2000">
<button type="submit" name = "submit" >Submit</button>
</form>

在output.php中,将值赋给数组。

if(isset($_GET['submit'])){
  $filters = array(
    array('filterType' => 'hotelStars', 'filterValue' => $_GET['hotelStars3']),
    array('filterType' => 'hotelStars', 'filterValue' => $_GET['hotelStars4']),
    array('filterType' => 'hotelStars', 'filterValue' => $_GET['hotelStars5']),
    array('filterType' => 'resultLimit', 'filterValue' => $_GET['resultLimit']));
  print_r($filters);