php - Getting x amount of variables from POST Array -
i'm wondering best way approach is. i'm working on registration form course. user specifies how many people registering , displays x amount of rows in table come in details each user.
this form submits php file handle data. if info post array need write out 100 statements or can dynamically.
for illustration 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 script 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 user selects quantity of 8 form contain 8 rows , when posted 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 utilize 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 have write x amount of variables check if set in post array?
if use
<input type="text" id="name" name="name<?= $j + 1 ?>"> then in php do:
$j = 0; while (++$j) { if (!isset($_post['name' . $j])) break; $name = $_post['name' . $j]; $email = $_post['email' . $j]; $phone = $_post['phone' . $j]; // ... } but advise remove loop , go arrays in html:
<input type="text" id="name" name="name[]"> then in php can do:
for ($j = 1; $j <= count($_post['name']); $j++) { $name = $_post['name' . $j]; $email = $_post['email' . $j]; $phone = $_post['phone' . $j]; // ... } php forms post http-post
No comments:
Post a Comment