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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form params; Same name... 1

Status
Not open for further replies.

Extension

Programmer
Nov 3, 2004
311
CA
Hi,

I'm parsing some form variables and in some cases the param name could be the same. If so, I would like to append the value.
Example:
Name is TYPE and value 1 = AT, value 2 = PT etc
So I want to end a value of AT,PT for the TYPE name.

Current code is not working....

Code:
# Convert FORM params into an Hash
foreach my $param ($cgi->param) {
	# Define Value
	$value = $cgi->param($param);
	
	# If name already exist, add comma and append value
	if ($input{$param}) {
		$input{$param} .= ",$value"; 
	}

	else {
		$input{$param} = $value; 
	}

 }

Thanks in advance.
 
$cgi->param by itself will give a list of the parameter names that have been passed to your script. Each name is only given to you once, so you're only ever setting $input{$param} once for each parameter, so your code to append will never be called (even if it is, calling $cgi->param($param) in scalar context will only give you the first value passed anyway).

To do this a nicer way, you can use a join:
Code:
foreach my $param ($cgi->param) {
    $input{$param} = join ',', $cgi->param( $param );
}
Parameters with more than one value will be joined with a comma. Others will just be by themselves.
 
Thank you ishnid for the explanation and the solution. As always, your help is really appreciated.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top