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

I have an inventory application using Mason. This particular form is for entering purchase orders. Our purchase orders consist of a generic block of details (date, vendor, buyer) and any number of line items.

Using an input for "How many line items does this PO have" I am able to generate a form that produces a table of line items exactly the length required. Example:

<tr> <th>description</th> <th>partno</th> <th>qty</th> <th>price</th> <th>currency</th> </tr> <tr> <td><input type=text name="description1"></td> <td><input type=text name="partno1"></td> <td><input type=text name="qty1"></td> <td><input type=text name="price1"></td> <td><input type=text name="currency1"></td> <input type=hidden name="podid1"></td> </tr> <tr> <td><input type=text name="description2"></td> <td><input type=text name="partno2"></td> <td><input type=text name="qty2"></td> <td><input type=text name="price2"></td> <td><input type=text name="currency2"></td> <input type=hidden name="podid2"></td> </tr>

What is the best way to pass all these variables to a mason component that does all the SQL work? How do I declair the variables qty1, qty2...etc in an <%args> if the number if them is always different?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: HTML forms and variable line items
by Tuppence (Pilgrim) on Aug 13, 2004 at 20:54 UTC
    you wrote:

    What is the best way to pass all these variables to a mason component that does all the SQL work? How do I declair the variables qty1, qty2...etc in an <%args> if the number if them is always different?

    my reply:

    The simple answer is, you don't. From the mason documentation:

    2. %ARGS hash: This variable, always available, contains all of the parameters passed to the component (whether or not they were declared). It is especially handy for dealing with large numbers of parameters, dynamically named parameters, or parameters with non-valid variable names. %ARGS can be used with or without an <%args> section, and its contents are unrelated to what you have declared in <%args>.

    Which means, that in your example you can acess qty2 with

    $ARGS{'qty2'}

    and you can find all your id's with

    my @ids = map { /^podid(\d+)/ ; $1 } grep { /^podid\d+/ } keys %ARGS;

    You might also look at making a request object and store a cgi instance on it, created with new CGI(%ARGS)