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?


In reply to dynamically adding forms by MCS

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.