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

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?

Replies are listed 'Best First'.
Re: (OT) HTML forms with ordered inputs
by Anonymous Monk on Nov 04, 2011 at 22:21 UTC