I’ve found myself with the following problem: I have form with a random number of fields. (This is so users can enter their fellow team members’s names — and there’s no set size for teams.) I need this inputted data to be formatted in preparation for being stored in a database.
The database structure is fine, it’s just getting a random number of $_POSTs and putting them into an array that’s the issue.
So far I’ve got some javascript that allows the user to create/remove an infinite number of fields, with incrementing ids/names (e.g. TeamMemberName1, TeamMemberEmail1, TeamMemberName2, TeamMemberEmail2, etc.).
Right now I’ve created an ugly series of PHP statements that puts a reasonable amount of POSTed data into a multidimenional array — I don’t see why a team should ever be bigger than 50 — and then I was going to process the array to remove any blank rows before passing it onto the database.
Example:
$submittedTeamMembers =
array ( "teamMember1" =>
array ( "name" => $_POST["teamMemberName1"],
"email" => $_POST["teamMemberEmail1"])
);
But I’m guessing there’s a better way!
Your input names can be in array form, which PHP will understand perfectly. Example:
which PHP will interpret after post as you’d expect: