Analysis of a form with an unknown number of fields

I am working on an invitation system in which a user can invite one or more people as coordinators for his account. To allow the user to invite more than one person at a time, I use mootools to create another row of fields whenever the user clicks the plus. In my example below, I will use only two lines, but they can invite much more if they want.

HTML

<table> <thead> <tr> <td>First Name</td> <td>Last Name</td> <td>Email Address</td> <td>Invites</td> </tr> </thead> <tbody id="coords"> <tr id="row-1"> <td id="1-1"><input type="text" name="fname-1"></td> <td id="1-2"><input type="text" name="lname-1"></td> <td id="1-3"><input type="text" name="email-1"></td> <td id="1-4"><input type="text" name="invites-1"></td> <td id="1-5"><span id="minus-1" class="minus">( - )</span></td> </tr> <tr id="row-2"> <td id="2-1"><input type="text" name="fname-2"></td> <td id="2-2"><input type="text" name="lname-2"></td> <td id="2-3"><input type="text" name="email-2"></td> <td id="2-4"><input type="text" name="invites-2"></td> <td id="2-5"><span id="plus-2" class="plus">( + )</span></td> </tr> </tbody> </table> 

$ _ POST looks like this:

 Array ( [fname-1] => David [lname-1] => Last [email-1] => email address [invites-1] => 15 [fname-2] => Shirley [lname-2] => Last [email-2] => other email [invites-2] => 10 [action] => invite ) 

PHP parsing is

 if (isset($_POST['action'])) { $_SESSION['inviteCoordinators'] = $_POST; $newCoordinators = array(); //loop through all fields while(list($key,$value) = each($_SESSION['inviteCoordinators'])) { if($key != 'action') { list($field, $user) = explode("-",$key); $newCoordinators[$user] = array ( $field => $value ); } } } 

I expected $ newCoordinators to look like this

 Array ( [1] => Array ( [fname] => David [lname] => Last [email] => email address [invites => 15 ) [2] => Array ( [fname] => Shirley [lname] => Last [email] => other email [invites => 10 ) 

But all I get is [1] [invites] => 15 and [2] [invites] => 10. I suppose I only see invitations because the others are being rewritten, but I can "I don’t understand why this is What happened to my analysis? Or, how should I do it?

Side note, the only required field will be an email address.

+4
source share
1 answer

Why not just name the elements of the html form using arrays? You can pretend something like:

 <table> <thead> <tr> <td>First Name</td> <td>Last Name</td> <td>Email Address</td> <td>Invites</td> </tr> </thead> <tbody id="coords"> <tr id="row-1"> <td id="1-1"><input type="text" name="invites[1][fname]"></td> <td id="1-2"><input type="text" name="invites[1][lname]"></td> <td id="1-3"><input type="text" name="invites[1][email]"></td> <td id="1-4"><input type="text" name="invites[1][invites]"></td> <td id="1-5"><span id="minus-1" class="minus">( - )</span></td> </tr> <tr id="row-2"> <td id="1-1"><input type="text" name="invites[2][fname]"></td> <td id="1-2"><input type="text" name="invites[2][lname]"></td> <td id="1-3"><input type="text" name="invites[2][email]"></td> <td id="1-4"><input type="text" name="invites[2][invites]"></td> <td id="2-5"><span id="plus-2" class="plus">( + )</span></td> </tr> </tbody> </table> 

and you would receive your message data formatted like this:

 Array ( [invites] => Array ( [1] => Array ( [fname] => first1 [lname] => last1 [email] => test@test.com [invites] => 2 ) [2] => Array ( [fname] => first2 [lname] => last2 [email] => test2@test.com [invites] => 5 ) ) ) 

Then you could just do something like:

 foreach($_POST['invites'] as $invite){ //process invite echo "{$invite['fname']} {$invite['lname']} was invited. <br />"; } 
+6
source

All Articles