I am currently putting together a web form in table fashion. Each row is a unique office location from the database. Each location has the same five columns, which consist of drop-down menus for user selection.
I would like to make this form as dynamic as possible--basically, not having to update the form processing code every time an office is added to the database, while also avoiding any hard-coded location information in the HTML. Since all the elements belong to the same form--and not separate forms--I'm running into problems. Currently, to identify form elements and to demarcate where one location's fields stop and another begins, I have element names like location01Employee, location01TimeIn, location01TimeOut, location01Coverage, location01Backup, location02Employee, location02TimeIn, location02TimeOut, location02Coverage, location02Backup, etc. The problem is that the form also is meant to handle different scenarios--sometimes, there will be 10 locations on the page, other times there will be 5.
How can I make this form process a variable number of locations and fields, but still uniquely identify all the form elements contained therein? It seems like what I really need to do is "variable-ize" the variable names themselves, so that the "01", "02" portions would be filled in as you go, but I'm guessing that PHP won't go for that.
Any suggestions on how to make this form as dynamic as possible and avoid all hard-coded maintenance? Any help would be appreciated.
Forms - Variable Number of Form Fields
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Is there a reason you aren't using databasing and arrays?
If you are, my apologies for assuming.
Anyway, you can easily generate the element names.
If you are, my apologies for assuming.
Anyway, you can easily generate the element names.
Code: Select all
<input name="location<?=$number;?>whatever" />Thanks for your reply.
Let's say that I have form elements named location1, employee1 ..... location10, employee10. Is there a way to load all the form elements into one giant array, but have the conents of each set (1-10) accessible together (e.g. for building an insert statement). In other words, how would I access all the variable1s in the array at once without hard-coding "1" anywhere?
Let's say that I have form elements named location1, employee1 ..... location10, employee10. Is there a way to load all the form elements into one giant array, but have the conents of each set (1-10) accessible together (e.g. for building an insert statement). In other words, how would I access all the variable1s in the array at once without hard-coding "1" anywhere?
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US