1

Issue 1: I have a form that contains a list of events pulled from a database, alongside which are checkboxes for customers to register their attendance. There is an additional checkbox to indicate any special requirements, as well as a text field to elaborate ('If yes, please provide additional details...').

I would like to store every event a customer is attending in a multi-dimensional array containing the event ID, 'yes/no' for special requirements and details of any such requirements. I thought I was on track with:

  <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>

... but when I do var_dump($_POST['registrations']);, I'm only getting:

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

The five-digit numbers are the event IDs, pulled from the database (I've registered for four events in this example), but the other info seemingly isn't being stored. I expect it's blindingly obvious, but can anyone spot where I'm going wrong?

I want to be able to loop through through the values (as per this example here) using the following code:

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

        // etc
    }

Issue 2: I would like to make sure that when the special requirements box is ticked, the details box is also filled out. Previously, I had three separate arrays for each part of the form, and did a count() on the number of ticks and the number of input boxes completed. If they didn't match, the form wouldn't process. However, I'm sure there's a much easier way of achieving this and would be grateful for any advice!

Community
  • 1
  • 1
Martin
  • 117
  • 2
  • 10
  • just use sql databases lol ,1 for ticked 0 for unticked ,and you add the info – Johny Mar 11 '13 at 14:08
  • **Issue 2** is a totally different and barely related question. I would suggest getting an answer for Issue 1, then (once you can properly describe your form) posting another question for Issue 2. – Ayman Safadi Mar 11 '13 at 14:16
  • Will do, thanks Ayman. I thought it might be something that could be fixed at (roughly) the same time but I'll post them one at a time. – Martin Mar 11 '13 at 14:27
  • I think this will solve your issue! http://stackoverflow.com/questions/10376774/html-form-passing-two-deminsion-array-to-php-special-case-i-think – abmmhasan Feb 12 '14 at 09:07

1 Answers1

1

When I try what I believe is your resultant HTML, I get the following from 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
                )

        )

)

Can you update your question with the HTML produced for the complete form?

Captain Payalytic
  • 1,061
  • 8
  • 9
  • Not with what I asked for you haven't! I asked for the "the HTML produced for the complete form". You have just added some more to what is in the php file. Please add the ACTUAL HTML PRODUCED. – Captain Payalytic Mar 11 '13 at 14:45