Reputation: 119
I'm working on an invite system where a user can invite one or more people to be coordinators for their account. To allow the user to invite more than one person at a time I'm using mootools to create another row of fields whenever the user clicks a plus. In my example below I'll be using just two rows but they could invite much more if they'd like.
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 this
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 was expecting $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 that I'm getting is the [1][invites] => 15 and [2][invites] => 10. I assume i'm only seeing the invites because the others are getting overwritten but I can't make sense of why that would be. What's wrong with my parsing? Or, how should I be doing it?
Side note, the only required field will be the email address.
Upvotes: 4
Views: 2493
Reputation: 15301
Why not just name your html form elements using arrays? You could make the form 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 get your post data already formatted like:
Array
(
[invites] => Array
(
[1] => Array
(
[fname] => first1
[lname] => last1
[email] => [email protected]
[invites] => 2
)
[2] => Array
(
[fname] => first2
[lname] => last2
[email] => [email protected]
[invites] => 5
)
)
)
Then you could just do something like:
foreach($_POST['invites'] as $invite){
//process invite
echo "{$invite['fname']} {$invite['lname']} was invited. <br />";
}
Upvotes: 7