Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Submitting form when drop down value changes

by bar10der (Beadle)
on Jul 05, 2005 at 10:55 UTC ( #472407=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,

I am using CGI-Application and HTML-Template to create a form where values from database are extracted and displayed to uers. My form requires 2 dropdown lists e.g. list1 and list2. Elements in list2 are controlled by selected value in list1.

So when user selects an element from list1, form is submitted and new values are populated in drop down list2 but elements in list1 goes back to default list order where as i want the selected item to be the 1st item in drop down. Here is html for drop down:-

<tr bgcolor="#ffffff"> <td class="colhead">List1</td> <td><select name="list1" size="1" style="background-color:#DDD;" onc +hange="admin.submit();"> <!-- TMPL_LOOP NAME=depts --> <option value="<!-- TMPL_VAR NAME=id -->"><!-- TMPL_VAR NAME=dn -- +> <!-- /TMPL_LOOP --> </select> </td> </tr> <tr bgcolor="#ffffff"> <td class="colhead">List2</td> <td><select name="list2" size="1" style="background-color:#DDD;" onc +hange="admin.submit();"> <!-- TMPL_LOOP NAME=emps --> <option value="<!-- TMPL_VAR NAME=id -->"><!-- TMPL_VAR NAME=ename + --> <!-- /TMPL_LOOP --> </select> </td> </tr> .... ....

Considered (gellyfish): delete or mark OT this is a Javascript Question
Unconsidered (holli): Enough Keep votes (Keep/Edit/Delete : 11/12/6)

Replies are listed 'Best First'.
Re: Submitting form when drop down value changes
by castaway (Parson) on Jul 05, 2005 at 11:38 UTC
    Guessing between the lines, is your problem: How to get the first list to display differently after selecting an item from it? - You'd need to resort the items in depts in your code, I'd guess.

    (Sorry don't know much about HTML::Template, maybe someone else does, if you explain your question better).

    C.

Re: Submitting form when drop down value changes
by Samy_rio (Vicar) on Jul 05, 2005 at 12:10 UTC

    Hi, If i understood your question correctly, here is my suggestion:

    I don't have much idea about Template and javascript "admin.submit()". so, I tried simple database and submit button.

    In the below code for list1, "if" part is run in first time and "else" part is run after submit the form.

    if ($q->param('list1') eq '') { print "<tr bgcolor=\"#ffffff\"> <td class=\"colhead\">List1</td> <td><select name='list1' size=\"1\" style=\"background-color:#DDD;\" +>"; while (my ($ca) = $sth1->fetchrow_array()) { print "<option value=\"$ca\">$ca</option>"; } print "</select> </td> </tr>"; } else { print "<tr bgcolor=\"#ffffff\"> <td class=\"colhead\">List1</td> <td><select name='list1' size=\"1\" style=\"background-color:#DDD;\" +>"; print "<option value=\"". $q->param('list1') ."\">". $q->param('list1 +') ."</option>"; print "</select> </td> </tr>"; }

    I think it helps you.

    Regards,
    Velusamy R.

Re: Submitting form when drop down value changes
by ghenry (Vicar) on Jul 05, 2005 at 11:14 UTC

    Did we miss your question?

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: Submitting form when drop down value changes
by cbrandtbuffalo (Deacon) on Jul 05, 2005 at 16:46 UTC
    In cases where we have done this, we just set the default value in the top list to what was selected. This way it comes up as selected, but the order of the drop-down doesn't change. Changing the order of the drop-down can be confusing for users, so you might want to consider this approach.

    I believe using the attribute 'selected' like so:

    <option value='stuff' selected>
    next to the one you want to select will do the trick. Note that the XHTML version of this is selected=selected.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://472407]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-09-17 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?