I’ve got a form that contains contact info. Each has a type, and then the contact information itself. For example:
Type | Contact
-------------------------------
cell | 555-555-5555
home | 444-444-4444
email | abc@def.com
It’s a functional requirement that users can add/remove contacts from the form using jQuery (showing only the contact info that exists – i.e. it’s not a static contact form).
Since users can add/remove any row they like in the form, this produces HTTP parameters similar to the following (with numbers that are not sequential, and may not be contiguous):
contact_ty3=cell
contact3=555-555-5555
contact_ty994=home
contact994=444-444-4444
contact_ty45=email
contact45=abc@def.com
Therefore, numbers after the contact and contact_ty parameters are arbitrary, except that they serve the purpose to match up the type to the associated contact information.
So, on the receiving end, in PHP, how to I efficiently process the parameters, using the numbers to match up the types and contacts? I’m thinking the best way to do this would be via regex, but the simplest function I can think of iterates through the entire $_REQUEST array, strips out the parameters that start with contact, and go from there.
Is there a more efficient way? Seems like I’m writing a manual string/param parsing method, when perhaps there’s a function to do this that I simply don’t know about. Since I won’t know the precise names of the form parameters beforehand, I can’t rely on the assumption that there will be a contact1, contact2, etc.
With some clever naming you can easily match them up on post:
And in PHP