Get both name and value from option using Perl CGI param
In my HTML code I have
<option name="PRODUCTS" value="3">Products</option>
I need get both the name and value values in my server side Perl function through my Perl CGI param.
I can only get the value, is there a way to get th开发者_运维知识库e name also?
<option name="PRODUCTS" value="3">Products</option>
An <option>
element does not have a name
attribute, so this is invalid HTML and the browser will ignore it (except that it might make it available to JavaScript).
When the form is submitted, the browser will send to the server the name of the select coupled with the value of the selected option. This is the only information that the server will receive.
If you want to get 'PRODUCTS' then you will need to either:
- Include it in the value:
value="3-PRODUCTS"
and thenmy ($number, $word) = split '-', $value
- Look up the word that
3
is related to on the server (e.g. in a hash embedded in the script, or with a database query).
CGI::param()
with no parameters returns all the names. Then CGI::param($name)
will return all the values for a given name.
for my $name (CGI::param()) {
for my $val (CGI::param($name)) {
print "name: $name, value: $val\n";
}
}
Update: sorry, misread "option" as "input". option's name attribute isn't sent to the server at all, so there's no way to tell what it was without also having the html itself. You could add javascript to set a hidden input field to the option's name when it's chosen, though. What does populateCatArray do?
I would use the Vars method if there is some doubts on what is getting passed, especially if you have thee same 'name' used multiple times or a js its producing elements that don't exist on the page normally.
use CGI;
use Data::Dumper
my $q = new CGI;
print
$q->header({ type=>'text/plain' }),
Dumper($q->Vars);
精彩评论