Dynamic Text Box Generation Help...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
madcat
Forum Newbie
Posts: 1
Joined: Tue Apr 13, 2004 9:18 am

Dynamic Text Box Generation Help...

Post by madcat »

Dynamic Text Box Generation Help...
Hi:
I need to ask for some help building a function that creates dynamic text boxes. This is an example link, I'd like this function to work EXACTLY the same way...just keep clicking 'Enter More Numbers':

http://www.ups.com/WebTracking/track?loc=en_US
*Just let me know if I need to put this in my profile.

The only difference is, I will have about 10 different sections of information that I'll need this function to work for. If the UPS site creates new boxes for 'Tracking', mine needs to create them for tracking, pin numbers, part numbers, mfg numbers, etc.

My aim is to somehow (my php skills are null) build one all purpose function for this. If 'Enter More Numbers' is clicked on my site I would like the boxes to be spit out in sets of 5 to 25 (if needed by user). That means that each name value (within generated text box) will need to be dynamically generated so that they could be input to a database.

Would it be easier to build a function for each section, or one all-purpose function. Also, what would be the best way to start building this all-purpose function? Here is what 2 out of the 10 sections might look like. The first five boxes are always visible for every section:

Tracking Number
-----------------
track1
track2
track3
track4
track5
----
Enter More Numbers Link >
Generate 5 More boxes until link is clicked again till 25

Pin Number
-----------------
pin1
pin2
pin3
pin4
pin5
----
Enter More Numbers Link >
Generate 5 More boxes until link is clicked again till 25

Any help is greatly appreciated as I feel way in over my head.

M
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hmm, it shouldn't be too hard to put all this work in a single function. I'd suggest that. Here's how I'd do it:

The link will pass two GET variables: The current number of text fields showing, and the category (PIN, tracking #, etc). Then, in the page proper, call the function using those variables. That function would then generate (the passed number + 5) fields, with the variable name corresponding to the category passed. You could also make the link a form button and pass all the variables as POST variables.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

If it were me, and this is just off the top of my head so it might not work or be the best way to do it, I would try to build either a function or (preferrably) a class to do this. The function/class would accept the name of the form element, as well as the number of elements you want, as arguments. It would then generate the form elements and name them with their position, as a number, appended to the element name (i.e. name="track1", etc.).

When the user clicks the link to add more fields, you pass the current number of fields in the ADD link using the GET method, along with some kind of identifier to let the function/class know which elements to increase the number of fields on. When the page reloads, check the variable and increment the number of fields accordingly.

Hope that makes sense/helps... :)
AznFuman
Forum Newbie
Posts: 6
Joined: Fri Apr 02, 2004 3:20 pm

Post by AznFuman »

you need a function
get the variables from before using a while loop.

while ($_POST[$a] != null && $_POST[$a] != "")
{
.................
}

and just use a for loop to create the text boxes.
Post Reply