当前位置: 动力学知识库 > 问答 > 编程问答 >

html - 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 the 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:

  1. Include it in the value: value="3-PRODUCTS" and then my ($number, $word) = split '-', $value
  2. 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);
分享给朋友:
您可能感兴趣的文章:
随机阅读: