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