Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How can I allow the user to enter multiple rows of data?

( #26443=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Aug 06, 2000 at 23:11 UTC
Q&A  > CGI programming


Description:

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.

Russ

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (10)
    As of 2014-07-30 19:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (240 votes), past polls