在HTML表单中使用多维数组

时间:2013-03-11 13:42:43

标签: php arrays forms

问题1:我有一个表单,其中包含从数据库中提取的事件列表,以及客户注册其出席的复选框。还有一个附加复选框,用于指示任何特殊要求,以及要详细说明的文本字段('如果是,请提供其他详细信息......')。

我想存储客户在包含事件ID的多维数组中参加的每个事件,对于特殊要求和任何此类要求的详细信息,“是/否”。我以为我正在跟踪:

  <form id="bookingform" method="post" action="booking-details.php">
    <fieldset>
      <legend>Personal information</legend>

      <div class="bookingitem">
        <label for="firstname">First name</label> <input type="text" name="firstname" id=
        "firstname" />*<br />
      </div>

      <div class="bookingitem">
        <label for="surname">Surname</label> <input type="text" name="surname" id=
        "surname" />*<br />
      </div>

      <div class="bookingitem">
        <label for="company">Company</label> <input type="text" name="company" id=
        "company" />*<br />
      </div>
    </fieldset>

    <fieldset>
      <legend>Which exhibition(s)?</legend>

      <div class="bookingitem">
        <?php
        $venues_query="SELECT exhibitions.exhib_id AS exhib_id, venues.venue_name AS venue, DATE_FORMAT(exhibitions.exhib_date, '%d/%m/%y') AS date FROM venues, exhibitions WHERE exhibitions.venue_id = venues.venue_id AND (exhibitions.exhib_date>=CURDATE())ORDER BY exhibitions.exhib_date";
        $venues_result=mysql_query($venues_query);
        while($venues=mysql_fetch_array($venues_result, MYSQL_ASSOC)){
        echo '<input type="checkbox" name="registrations['.$venues['exhib_id'].'][id]" /> '.$venues['venue'].' - '.$venues['date'].'<br/>';
        echo '<input type="checkbox" name="registrations['.$venues['exhib_id'].'][requirements]" />Special requirements?<br/>';
        echo 'If yes, please give more details... <input type="text" name="registrations['.$venues['exhib_id'].'][requirements_details]" />';
        }
        mysql_close();
        ?>
      </div>
    </fieldset>

    <fieldset>
      <legend>Terms and conditions:</legend>

      <p>T&amp;Cs here</p>

      <div>
        <input type="submit" class="buttons" name="submit" value="Submit" />
      </div>
    </fieldset>
  </form>

...但是当我做var_dump($_POST['registrations']);时,我只会:

array(4) { [0]=> string(5) "00132" [1]=> string(5) "00140" [2]=> string(5) "00135" [3]=> string(5) "00136" }

五位数字是从数据库中提取的事件ID(在本例中我注册了四个事件),但其他信息似乎没有被存储。我希望它显得非常明显,但是有人可以找到我出错的地方吗?

我希望能够使用以下代码遍历值(as per this example here):

foreach ( $_POST['registrations'] as $registration )
    {
        echo '$registration[id]';
        echo '$registration[requirements]';
        echo '$registration[requirements_details]';

        // etc
    }

问题2:我想确保勾选特殊要求框时,还会填写详细信息框。以前,我为表单的每个部分都有三个单独的数组,并且在tick的数量和输入框的数量上完成了count()。如果它们不匹配,表单将不会处理。但是,我确信有一种更简单的方法可以实现这一目标,并对任何建议表示感谢!

1 个答案:

答案 0 :(得分:1)

当我尝试我认为是你的结果HTML时,我从print_r得到以下内容:

Array
(
    [registrations] => Array
        (
            [11111] => Array
                (
                    [id] => on
                    [requirements] => on
                    [requirements_details] => 1
                )

            [22222] => Array
                (
                    [id] => on
                    [requirements] => on
                    [requirements_details] => 2
                )

            [33333] => Array
                (
                    [id] => on
                    [requirements] => on
                    [requirements_details] => 3
                )

            [44444] => Array
                (
                    [id] => on
                    [requirements] => on
                    [requirements_details] => 4
                )

            [55555] => Array
                (
                    [id] => on
                    [requirements] => on
                    [requirements_details] => 5
                )

        )

)

您可以使用为完整表单制作的HTML更新您的问题吗?