Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

multiple inputs on form with same name

by c (Hermit)
on Oct 16, 2001 at 18:03 UTC ( [id://119127]=perlquestion: print w/replies, xml ) Need Help??

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

i am dealing with a form which has a credit card entry field that is actually broken up into four input fields all having the same name value.

<input type="text" maxlength="4"name="17cardnumber">&nbsp; <input type="text" maxlength="4"name="17cardnumber">&nbsp; <input type="text" maxlength="4"name="17cardnumber">&nbsp; <input type="text" maxlength="4"name="17cardnumber">&nbsp;

when i parse the input from the form using cgi.pm, each value writes over the next, leaving me with only the digits entered for the final field as my value.

## grab form input my $query = CGI->new(); my @formfields = $query->param; for my $field(@formfields) { $FORM{$field} = $query->param($field); }

my copy of dr. stein's book is out of town at the moment(seriously) and i seem to recall some chapter mentioning this effect. can someone refresh my memory?

humbly -c

Replies are listed 'Best First'.
Re: multiple inputs on form with same name
by arturo (Vicar) on Oct 16, 2001 at 18:28 UTC

    The trick is to make sure the call to param occurs in a list context:

    my @cardnumbers = $q->param("17cardnumber");

    This is, by the way, documented in the POD for CGI.pm (perldoc CGI).

    Books (and supporting authors who've contributed so much) are nice, but they don't update when the module updates =)

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: multiple inputs on form with same name
by pitbull3000 (Beadle) on Oct 16, 2001 at 18:22 UTC
    AFAIK when you have a form with multiple fields with the same name, you will get an array, in your case with 4 entrys. try to get your entrys with
    my $query = new CGI; my @formfields = $query->param('17cardnumber'); my $entryOne = $formfields[0]; my $entryTwo = $formfields[1]; my $entryThree = $formfields[2]; my $entryFour = $formfields[3];
Re: multiple inputs on form with same name
by tommyw (Hermit) on Oct 16, 2001 at 19:13 UTC

    As the others say, you're getting an array returned. The real problem is the order you want the fields back: there's no guarantee that it's the same order as you've drawn them on the form! (and if one's blank, you can't tell which: you only know that you're only passed three fields.)

    This really is a case for dynamically constructing the field names (note, that's symbolic field names, not symbolic variable names): 17cardnumber_1 through 17cardnumber_4, for example

Re: multiple inputs on form with same name
by hopes (Friar) on Oct 16, 2001 at 18:35 UTC
    Hi,
    when you code
    $FORM{$field} = $query->param($field);
    you are asigning $query->param($field) to an scalar, but it is an array, so you are obtaining the last element of the array.

    In CGI documentation you can find
    @foo = split("\0",$params->{'foo'});

    but I would code this:
    @array = $query->param($field);

    and you will have an array with the parameters.

    Hope this helps
    Hopes
Re: multiple inputs on form with same name
by Elliott (Pilgrim) on Oct 16, 2001 at 20:43 UTC
    I've coped with this by writing my own form parser (in defiance of all the good advice here). I n my defence, it was when I first started writing in Perl and I hadn't heard of modules!

    Anyway, I am replying to give you a completely different take on this. Personally, I loathe credit card forms where you have to enter the different elements into different fields. I regard tht as user-hostile. The credit card captures I write allow you to enter the whole number into one field and delimit the sections with spaces, hyphens or not at all.

    Same goes for the expiry date. Either offer two drop downs for month and year (remembering to have the years move forward automatically relative to the current year) or a single field that will accept dates as 2/02, 2/2002, 0202, 02/02, 02.02 02 02, etc etc. (Drop downs are a lot easier!)

      very good

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://119127]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found