This is a little project I started with the idea of trying to teach myself a little about CGI. It seemed to be going okay until I hit this snag. I'm not sure which information will be helpful so I'm going to err on the side of verbosity.

My script is creating a table in HTML based on a series of hashes read in from text files. The idea being that if the items in the table change, I just change the text file instead of the code. I am writing each table row by means of a foreach loop on an array list of the items. Here's a snippet:

foreach (@inventory) { print "<TR>"; print "<TD>$_</TD>"; print "<TD>$inv_prices{$_}</TD>"; print "<TD>$inv_weights{$_}</TD>"; print "<TD><input type=\"text\" size=\"4\" name=\"quantity$_\" ></ +TD></TR>" }

OK, my problem is with "quantity$_". That's the field that the user would actually change. When you enter a quantity into the field and press the SUBMIT button, the next script can't seem to recognize it as a parameter unique to a given item and prints nothing in the "shopping cart." Here's the code snippet from the 2nd script:

foreach (@inventory) { my $qty = param("quantity$_"); $qty{$_} = $qty } foreach (@inventory) { print "<TR>"; print "<TD>$_</TD>"; print "<TD>$inv_prices{$_}</TD>"; print "<TD>$qty{$_}</TD></TR>"; }

Nothing comes out on the page, though. If I leave out the code relevant to "quantity$_" and the %qty hash, then the rest formats fine.

How do I set up the parameter for the quantity field in the first script so that it changes for each item and get the 2nd script to read that parameter properly? Or am I just going about this in completely the wrong way? Thanks.


In reply to CGI problem: trying to assign parameters in a loop by bighara

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.