Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

dynamically adding forms

by MCS (Monk)
on Mar 02, 2004 at 17:32 UTC ( [id://333310]=perlquestion: print w/replies, xml ) Need Help??

MCS has asked for the wisdom of the Perl Monks concerning the following question:

I'm creating a little web based recipe database program and I have the majority of it done and working. I initially created a script to parse existing txt files (and thanks to antiword, word files) and insert them into the database. I then added a editform.pl cgi script to edit the values in case something went wrong. Now I want to add the ability to enter in a new recipe via a web form. Now I could have two text boxes and then parse the ingredients but that can be prone to errors when things don't follow the standard qty, amt, description (like 1 cup sugar). Therefore I want to do fields for the qty (1) amt (cup) and description (sugar). However there is no telling how many ingredients there are.

I thought the best way to handle this would be to have a few fields up and if they are blank, just not use them. However, if the user has a long list of ingredients, I would like the ability to add a set of three text fields. What would be the best way to go about this?

My edit recipe forms look like this:

print "Qty:", $query->textfield(-name=>"qty$ingredientAoH[$i]{ingID}", -default=>"$ingredientAoH[$i]{ +qty}", -size=>'10', -maxlength=>'20', "\n"); print "Amt:", $query->textfield(-name=> "ammount$ingredientAoH +[$i]{ingID}", -default=> "$ingredientAoH[$i] +{amt}", -size=>10, -maxlength=>100, "\n"); print "Des:", $query->textfield(-name=> "description$ingredien +tAoH[$i]{ingID}", -default=> "$ingredientAoH[$i] +{des}", -size=>50, -maxlength=>100), "\n"; print $query->hidden(-name=>"ingID$ingredientAoH[$i]{ingID}", +-default=>"$ingredientAoH[$i]{ingID}"), "<br>\n";
Just some references: the $ingredientAoH is an array of hashes where qty, amt and des are the three keys and each element of the array indicates a seperate ingredient. Then there is also the ingID key which is a unique ingredient key from the database... it is naturally hidden from the user and is just used for updating the database.
I call each field qtyn, ammountn, descriptionn where n is the ingID. (Used to tell which one's which)

Should I just have a parameter indicating the number of fields and then just increment that on a add ingredient button and reload the form? If so how do I keep already existing values from disapearing? Should I pass my ingredientsAoH as a parameter?

Replies are listed 'Best First'.
Re: dynamically adding forms
by BUU (Prior) on Mar 02, 2004 at 21:13 UTC
    The simplest/easiest would be to just offer a text field called "number of items" that submits to the next page which has the actual recipe submission, preferably via get.

    After that you can just have a button at the bottom that looks like <input type='submit' name='add_item' value='Add Ingredient'> and when the form is submitted check for param('add_item'), and if it's found simply redisplay the form with the elements already submitted redisplayed.

    (Btw, I suggest giving all the ingredients the same "form name", that way the various cgi-processing modules will give you all of the ingredients as a list. Although I'm not sure what order the list is in.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://333310]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found