lanta has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have a web input form and the form has a SELECT input element. I'm using the CGI module to get the values selected in the SELECT element.

@selected_options = $cgi->param( 'select_name' );

@selected_options would typically contain the values of selected options( like 1,2,3 etc ). I'm getting the string "[ object HTMLInputElement ]", which I guess is the stringified Option object.

I should mention that the SELECT options have been dynamically added using the following Javascript function.

function addOption(object,text,value) { var optionName = new Option(text, value) object.options[object.length] = optionName; object.options[object.length-1].selected = false; }

Is it possible to add the option in a different fashion so that param would return the value or is there a different way to use param() to get the value from the Option object.

Thanks in advance for your time.

Replies are listed 'Best First'.
Re: CGI param returns HTMLInputElement.
by aquarium (Curate) on Oct 01, 2007 at 03:28 UTC
    if you're trying to ajaxify your app..perhaps do away with form submit (which causes a full page reload) and use JSON style messages instead. there'll be a JSON/perl toolkit or two about
    the hardest line to type correctly is: stty erase ^H
Re: CGI param returns HTMLInputElement.
by Gangabass (Vicar) on Oct 01, 2007 at 01:27 UTC

    Can you show what is the value of object param in your Javascript code? Maybe there is some Javascript function which change values of SELECT onSubmit?

      Thanks Gangabass, you are right. The problem was on the javascript side.

      I was calling the addOption() as follows

      addOption(someSelectObject,"display_string", someTextObject);
      instead of
      addOption(someSelectObject,"display_string", someTextObject.value);
Re: CGI param returns HTMLInputElement.
by balaji_red83 (Acolyte) on Oct 01, 2007 at 06:51 UTC
    Can you try using the following function,
    function addOption(object,text,value) { object.options[object.options.length] = new Option(text, value); }
      Thanks balaji_red83, that was not the probem, but you're right, no need for the extra variable and the extra statement. Duly noted.
Re: CGI param returns HTMLInputElement.
by stark (Pilgrim) on Oct 01, 2007 at 11:16 UTC

    I use Firebug (Firefox extension) to get insight...

    It is a great helper when progamming ajax. It even includes a JavaScript debugger.

      sadly, Firebug/Firefox are too forgiving.

      E.g., a statement like
      var foo = { bar : 'baz', };
      runs perfectly in FF but not in other browsers (the last comma is wrong). This is especially annoying when you come from Perl and are even encouraged to write you hashes like
      $foo = { bar => 'baz', };


      holli, /regexed monk/
      Thanks stark, it looks pretty slick, I guess now I can stop using all those alert() ;-)
Re: CGI param returns HTMLInputElement.
by Anonymous Monk on Oct 01, 2007 at 03:24 UTC
    debug your own Javascript
      Thanks Anonymous Monk, the problem was indeed on the Javascript side.