Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting form elements

Status
Not open for further replies.

damann

Technical User
Jul 19, 2000
114
US
How do I get the form elements from all my form fields and store them into an array using the cgi module?

@elements = $query->param();

this is the way I know how, and this only outputs the name of all the fields instead of what the user typed in them. What else is needed to make this possible....
 
'not sure why you want a list of the values w/o the keys to go along with them, but...
[tt]
# for the name=values pairs...
# get a list of the names
@names = $query->param();

# build a list of the values
foreach $name (@names) { push @values, $val; }
[/tt]

'hope this helps.
 
No that doesn't work... I dont understand, there's got to be a way to get all the values the user entered in my form into an array.

if ( $ENV{'REQUEST_METHOD'} eq "GET" &&
$ENV{'QUERY_STRING'} ne '') {
$buffer = $ENV{'QUERY_STRING'};
}
elsif ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
read(STDIN,$buffer, $ENV{'CONTENT_LENGTH'});
}

@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($FORM{$name}) {
$FORM{$name} = "$FORM{$name}, $value";
}
else {
$FORM{$name} = $value;
}
}

the code above does it without using CGI.pm but I would like to know how to do it with the module instead of writing this all the time.

$vals = $query->param('username');

this gets the value of the user's username...just one line of code. Now if I have a huge form with say 30 input fields, I dont want to have to do that 30 times. So there's got to be a way to get all the user input, and just as simple. Need help...Please help...

 
In you original post, you asked how to get the form inputs into an array. An array is usually interpretted to mean a list. In your second post you appear to be handling the inputs to get them into a hash (an associative array, not a simple array). If you are, in fact, trying to get the form inputs into a hash, then, you are creating redundant code. The $query object already has the inputs stored in a hash structure. If you are trying to do it to eliminate some of the typing associated with
this syntax -- $name = $query->param('name'); --
then,
[tt]
# for the name=values pairs...
# get a list of the names
@formInputs = $query->param();

# build a hash of the values
foreach $key (@formInputs) { $newHash{$key} = $query->param{$key}; }

# now you can...
$nameValue = $newHash{'name'};
[/tt]

Note that this is a completely redundant memory structure.

'hope this helps....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top