For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > October 2005 > Taking Multiple Values for The Same Form Field.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Taking Multiple Values for The Same Form Field.
Sara

2005-10-29, 3:55 am

Never thought of it before, but now I need help as how to pass multiple values of a form field with a single submit to script.

****** example ******

<form>

<!-- Name is a required Field, but E-mail could be left blank -->

<!--- First Input -->
<input type=text name=name>
<input type=text name=email>

<!-- Second Input -->
<input type=text name=name>
<input type=text name=email>

<!-- Third Input -->
<input type=text name=name>
<input type=text name=email>

<input type=submit value="Submit">

</form>

************************************

use CGI;
my $q = new CGI;

my $name = $q->param('name');

Now What as multiple name values will be coming.............??


Thanks for any help.

Sara.
Ovid

2005-10-29, 3:55 am

--- Sara <sara.samsara@gmail.com> wrote:

>
> use CGI;
> my $q = new CGI;
>
> my $name = $q->param('name');
>
> Now What as multiple name values will be coming.............??


Use list context:

my @names = $q->param('name');

Cheers,
Ovid

--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Sara

2005-10-29, 3:55 am

Thanks for the quick respons, just need a bit more help. Now I need to write
these values in a text file (I am fully aware of writing to files) but where
I am gettin' is that : I told you that Name is a required field,
but Email is optional. so what if :

my @name = ('Sara', 'John', 'Doe'); # 3 names coming from form.
my @email = ('John@John.com'); # coming from form as Email Optional field is
ONLY filled for John.

The problem is $name[0] does not corresponds with $email[0] and so on....
Otherwise I would have used while or foreach for @name and @email to write
it to file like it:

### TEXT FILE TO WRITE #######
Sara |
John | John@John.com
Doe |
##############################

Thanks again for your help.

Sara.



----- Original Message -----
From: "Ovid" <publiustemp-beginnerscgi2@yahoo.com>
To: <beginners-cgi@perl.org>
Sent: Saturday, October 29, 2005 9:14 AM
Subject: Re: Taking Multiple Values for The Same Form Field.


> --- Sara <sara.samsara@gmail.com> wrote:
>
>
> Use list context:
>
> my @names = $q->param('name');
>
> Cheers,
> Ovid
>
> --
> If this message is a response to a question on a mailing list, please send
> follow up questions to the list.
>
> Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


Ovid

2005-10-29, 3:55 am

--- Sara <sara.samsara@gmail.com> wrote:

> Thanks for the quick respons, just need a bit more help. Now I need
> to write
> these values in a text file (I am fully aware of writing to files)
> but where
> I am gettin' is that : I told you that Name is a required
> field,
> but Email is optional. so what if :
>
> my @name = ('Sara', 'John', 'Doe'); # 3 names coming from form.
> my @email = ('John@John.com'); # coming from form as Email Optional
> field is
> ONLY filled for John.
>
> The problem is $name[0] does not corresponds with $email[0] and so
> on....
> Otherwise I would have used while or foreach for @name and @email to
> write
> it to file like it:


As of CGI.pm version 2.63, if the name is supplied in the query string
but has no value, CGI.pm should return an empty string for that. This
should let you keep your @name and @email arrays in synch. Even if you
are using a POST, there should be a query string in the entity-body and
CGI.pm *should* handle this correctly.

Cheers,
Ovid

--
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/
Randal L. Schwartz

2005-10-29, 6:58 pm

>>>>> "Ovid" == Ovid <publiustemp-beginnerscgi2@yahoo.com> writes:

Ovid> As of CGI.pm version 2.63, if the name is supplied in the query string
Ovid> but has no value, CGI.pm should return an empty string for that. This
Ovid> should let you keep your @name and @email arrays in synch. Even if you
Ovid> are using a POST, there should be a query string in the entity-body and
Ovid> CGI.pm *should* handle this correctly.

.... unless the browser is returning only one email value (because the
other two are not touched), which is quite possible.

If you need to correlate fields, use distinct names:

name1 email1
name2 email2
name3 email3

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Bob O'Neill

2005-10-29, 6:58 pm

<<Use list context:

=A0 my @names =3D $q->param('name');

Cheers,
Ovid[color=darkred]


I've found this thread very helpful, and have changed it to use Vars (from =
=20
http://users.easystreet.com/ovid/cgi_course/) as I need a hash. This has=20
replaced the following:

foreach $entry (param()) {
$fieldhash{$entry}=3Dparam($entry);

Checkentry(param($entry));
}

with:

my $q=3Dnew CGI;
my %fieldhash=3D$q->Vars;

which is much neater. My subroutine "Checkentry" untaints the input. Is=20
there an easier way which will allow the hash values created by Vars to be=
=20
passed to my subroutine without stepping through a "foreach" loop?=20

Bob

Wiggins d'Anconia

2005-10-29, 6:58 pm

Bob O'Neill wrote:
> <<Use list context:
>
> my @names = $q->param('name');
>
> Cheers,
> Ovid
>
>
>
> I've found this thread very helpful, and have changed it to use Vars (from
> http://users.easystreet.com/ovid/cgi_course/) as I need a hash. This has
> replaced the following:
>
> foreach $entry (param()) {
> $fieldhash{$entry}=param($entry);
>
> Checkentry(param($entry));
> }
>
> with:
>
> my $q=new CGI;
> my %fieldhash=$q->Vars;
>
> which is much neater. My subroutine "Checkentry" untaints the input. Is
> there an easier way which will allow the hash values created by Vars to be
> passed to my subroutine without stepping through a "foreach" loop?
>


If I understand correctly then you want a hash reference to pass the
whole hash around at once.

perldoc CGI

Should help, C<Vars> understands its context, so calling it in scalar
context will result in it returning a hashref instead of a hash.

my $params_ref = $q->Vars;

http://danconia.org


> Bob
>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com