Dear monks,

I apologise in advance -- my problem is not too related to Perl, but rather HTML forms and how to deal with them server-side.

Suppose I have a form where the user is expected to input a track listing for an audio CD, with possible supplementary data, such as track length. How should I go about naming the form elements so that I can process them easily on the server-side?

I understand that if you give the form inputs the same name ('input name="tracktitle"'), you can get the parametres in an array with CGI.pm and other modules, but the browsers are not required to keep the ordering, and thus you cannot match the track title and length.

Also, what about editing? If the track listing data is in the database, how should I build a form so that I can best match the user's corrections with the data in the database? (Each track has a unique ID I can use, FWIW)

My try so far is

<input name="tracktitle_1" type="text"> <input name="tracklength_1" type="text"> <input name="tracktitle_2" type="text"> [...]

However, the code to enumerate through that is far from elegant, something akin to:

$i = 0; while (1) { $i++; $title = get_param("tracktitle_$i"); last unless defined $title; $length = get_param("tracklength_$i"); ... insert into database ... }

Are there any better methods?


In reply to (OT) HTML forms with ordered inputs by Anonymous Monk

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.