Contributed by Anonymous Monk on Aug 06, 2000 at 23:11 UTC
Q&A  > CGI programming


Let's say that a user has to insert records into a table everyday, 10 records at at time.

Instead of making a form for a single record and have the user click on submit many times, wouldn't it be nice if the user was presented with 10 rows of input boxes and have him click on submit only once?

Answer: How can I allow the user to enter multiple rows of data?
contributed by Russ

Yes, it would.

In the "output" side of your web script:

# Give each row a unique value (1, 2, 3...) for (1..10){ # Name each field, adding the unique value in $_ (A1, B1, C1...) print qq{ <tr> <td><input type="text" name="A$_"></td> <td><input type="text" name="B$_"></td> <td><input type="text" name="C$_"></td> <td><input type="text" name="D$_"></td> </tr> }; }
In the "processing" side:
for (1..10){ my ($A, $B, $C, $D) = (CGI::param("A$_"), CGI::param("B$_"), CGI::param("C$_"), CGI::pa +ram("D$_")); # This line confirms that the user entered something in at least one + field next unless grep {defined $_} ($A, $B, $C, $D); # Now that we know we got user input, do an insert into the database }
This is not the most efficient way to do things, but I'm trying to demonstrate the technique, not give cut-and-paste-ready code.

Good luck.


Answer: How can I allow the user to enter multiple rows of data?
contributed by rodry

Russ's solution seems to accomplish this by having the MySQL insert the records one at a time. I know MySQL has a feature that let's you do an Insert of multiple rows with one statement.

How do you accomplish that.

Answer: How can I allow the user to enter multiple rows of data?
contributed by merlyn

If the param names were sequential, they'd be easier to loop. :)

use CGI ":all"; # of course # ... # generate HTML: print p("Field $_:"), textfield("input$_"), br for 1..10; # ... # get params: $data[$_] = param("input$_") for 1..10;

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.