I'm wondering what the best way to approach this is. Basically I'm working on a registration form for a course. The user specifies how many people will be registering and this displays X amount of rows in the table to enter details for each user.
This form submits to a php
file that will handle the data. However if I am to get the information from the POST
array do I need to write out 100 statements to do this or can I do it dynamically.
For example currently this is my form:
<form action="#" method="POST">
<table>
<th></th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<? for($i = 0; $i < $qty; $i++):?>
<tr><td>Guest <?= $i + 1?></td>
<td><input type="text" id="name" name="name<?= $i + 1 ?>"></td>
<td><input type="text" id="email" name="email<?= $i + 1 ?>"/></td>
<td><input type="text" id="phone" name="phone<?= $i + 1 ?>"/></td>
<input type="hidden" name="course_id" value="<?= the_ID() ?>"/>
<input type="hidden" name="course_title" value="<?= $course ?>"/>
<input type="hidden" name="formSend2" value="1" />
<input type="hidden" name="course_date" value="<?= $date ?>"/>
<input type="hidden" name="course_location" value="<?= $location ?>"/>
<input type="hidden" name="course_applicant" value="<?= $user_ID ?>"/>
</tr>
<? endfor; ?>
</table>
<input type="submit" value="Confirm Registration"/>
</form>
This is the script it submits to
function register_course_handler() {
var_dump($_POST);
die();
$course_id = $_POST['course_id'];
$course_title = $_POST['course_title'];
$course_date = $_POST['course_date'];
$course_location = $_POST['course_location'];
$course_applicant = $_POST['course_applicant'];
If the user selects a quantity of 8 the form will contain 8 rows and when posted the var_dump($_POST)
outputs this:
array (size=30)
'name1' => string 'd' (length=1)
'email1' => string 's' (length=1)
'phone1' => string 'd' (length=1)
'course_id' => string '1063' (length=4)
'course_title' => string 'Energy use in Mushroom Units' (length=28)
'formSend2' => string '1' (length=1)
'course_date' => string '23-07-2014' (length=10)
'course_location' => string 'Teagasc, Thurles' (length=16)
'course_applicant' => string '1' (length=1)
'name2' => string '' (length=0)
'email2' => string '' (length=0)
'phone2' => string '' (length=0)
'name3' => string '' (length=0)
'email3' => string '' (length=0)
'phone3' => string '' (length=0)
'name4' => string '' (length=0)
'email4' => string '' (length=0)
'phone4' => string '' (length=0)
'name5' => string '' (length=0)
'email5' => string '' (length=0)
'phone5' => string '' (length=0)
'name6' => string '' (length=0)
'email6' => string '' (length=0)
'phone6' => string '' (length=0)
'name7' => string '' (length=0)
'email7' => string '' (length=0)
'phone7' => string '' (length=0)
'name8' => string '' (length=0)
'email8' => string '' (length=0)
'phone8' => string '' (length=0)
Do I have to write x amount of variables to check if they are set in the POST
array?