1

问题 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(在此示例中我注册了四个事件),但似乎没有存储其他信息。我希望这是非常明显的,但是任何人都可以发现我哪里出错了吗?

我希望能够使用以下代码循环遍历这些值(根据这里的示例):

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

        // etc
    }

问题2:我想确保当特殊要求框被勾选时,详细信息框也被填写。以前,我为表单的每个部分设置了三个单独的数组,并对count()刻度数和已完成的输入框数进行了分析。如果它们不匹配,则不会处理该表单。但是,我确信有一种更简单的方法可以实现这一目标,并且将不胜感激任何建议!

4

1 回答 1

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 更新您的问题吗?

于 2013-03-11T14:15:07.207 回答