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

I've got a drop-down menu in CGI.pm that I want to submit the form with the new value whenever a user changes the value of the drop-down box.

I've read the perldoc on CGI.pm several times and, while it says to use the -onChange parameter, I don't know how. A friend said to add "-onChange => 'submit'," to my drop-down menu definition, but that didn't do anything.

Help?

  • Comment on JScript event handles and CGI.pm dropdown menus

Replies are listed 'Best First'.
Re: JScript event handles and CGI.pm dropdown menus
by HyperZonk (Friar) on Jul 20, 2001 at 23:00 UTC
    The -onChange parameter only sets up the drop-down to call a JavaScript method, it doesn't write the method for you. You will want -onChange=>'mySubmit()' as a parameter, and have the CGI emit a JavaScript at the beginning of the document. Assuming that you assign name="theForm" in the form tag, the JavaScript might be something like:
    function mySubmit() { document.theForm.submit() }

      Thank you! That's exactly what I needed to know to go forward. *bounces happily around the cube*
Re: JScript event handles and CGI.pm dropdown menus
by synapse0 (Pilgrim) on Jul 20, 2001 at 22:57 UTC
    You don't have to use the CGI.pm code all the time. For something like this, where you can't seem to get it to work and the docs aren't clear (dunno if they are or not, i've not looked the info up for your question) you can always just print the info out manually, or, if you call it a lot, write a small function for it. Usually CGI.pm just takes something like -option=>value and turns it into option=value in a tag. Personally, I usually write my own HTML, instead of relying on CGI.pm to do it for me. Two of the plusses for rolling your own is control of the text and formatting.
    -Syn0
Re: JScript event handles and CGI.pm dropdown menus
by voyager (Friar) on Jul 21, 2001 at 00:44 UTC
    An alternative is to have the javascript do something like window.location = "myURL".

    <soapbox>Please note that a user expects the page to change when clicking a button or link, but not when choosing from a SELECT. Do you really want your page to behave differently (therefore unexpectedly) than most of the rest of the web?</soapbox>

      To reply to the soapbox (so that I don't seem like a greater fool than I really am) ...

      I'm mimicking an application that exists already, written in Perl/Tk. That application does this behavior. In addition, this application is used solely for internal purposes.

      Plus, Yahoo and Netscape already use the behavior I'm describing. *shrugs* I'm not breaking new ground.

      I agree, your soapbox position may be in accordance with the purist's view on how HTML SHOULD be used. However, the behaviour of dropdown menus to take the person to the page when they choose it has now become pretty much a standard across the web. And pause to think about it for a minute or two - the average person would take that behavious to be logical - they select the option they want, so why should they have to then click on a button? One action rather than two.

        Because you can't make all drop-downs work that way, and the user shouldn't have to guess how it works. For example, when filling out your address, the STATE may be a drop-down. Or for setting scope for a search. The search won't happen until you click the button.

        Now if there's a message that says something like "choose a destination" (or whatever), then fine. Or if it's an intRAnet, make your own rules. But if someone's not paying attention and the page changes and they don't realize it, it's trouble.

        I've even run into this when I move the wheel on my mouse when I happen to be in one of these "touchy" selects. The page goes crazy :)

        Tastes vary.