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

Hi Monks, I'm learning cgi and I was wondering, if a person selects a data with in a select field for example
<select name="color1" multiple size=3> <option value="red"> Red <option value="green"> Green <option value="blue"> Blue <option value="gold"> Gold </select>
how can I process the data automatically once the user selects the value without sending hitting a submit button?
  • Comment on Cgi and Perl : how to use data once people selects the checkbox field?
  • Download Code

Replies are listed 'Best First'.
Re: Cgi and Perl : how to use data once people selects the checkbox field?
by bgreenlee (Friar) on Dec 17, 2004 at 16:48 UTC
    You need to use Javascript to do that. You'd have an 'onclick' handler that then checks the state of your checkboxes.

    -b

Re: Cgi and Perl : how to use data once people selects the checkbox field?
by punkish (Priest) on Dec 17, 2004 at 18:16 UTC
    To do something on the client (the browser) side, you have to use JavaScript.

    If you want to simply avoid the user having to hit the "submit" button, you can

    <select onChange="document.form.submit();">

    and then things will be sent to the server where you can use Perl to do more stuff with the values.

    Otoh, if you want to do more magic on the client, you can

    <select onChange="domagic();">
    and put your magic logic in the JavaScript function in domagic.

    Update: The node should be retitled. It currently says "Re: Cgi and Perl : how to use data once people selects the checkbox field?", but the OP is showing the select field; in other words, it could be "how to use data once people choose a value in the select field." Javascript methods for dealing with select widgets are different from those dealing with checkboxes.

Re: Cgi and Perl : how to use data once people selects the checkbox field?
by amt (Monk) on Dec 17, 2004 at 16:59 UTC
    This is a very basic example, so you will have to fix for your specific needs, you can try irt.org if you need to ask more indepth javascripting questions.
    <select onchange='foo();'> <option value=1>trival example</option> </select> <script type='text/javascript'> function foo () { form.submit)(; return true; }
    amt.

    perlcheat
        how can do I do it such that I have multiple forms?
Re: Cgi and Perl : how to use data once people selects the checkbox field?
by nedals (Deacon) on Dec 18, 2004 at 03:27 UTC
    Now that you know how to submit the 'data', you will need to retrieve it in your CGI script.
    use strict; use CGI; my $query = new CGI; ## This will get the 'value' my $selected_value = $query->param('color1');

    Update:
    I just noticed that you are using 'multipule' in your select.
    The onchange event will not allow you to select multipule items.
    You will need a seperate 'submit' button for that to work.

    If you need multipule 'values', use this to put the values into an array.
    my @selected_value = $query->param('color1');