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

Hi Monks, can anybody help? I have a perl cgi form and I would like to have a drop down list, that displays a text box if the selection 'other' is chosen.
$html .= popup_menu ( -name => $obj, + -values=>\@machines);
The values array has a list of hosts in it and one of those is other, I would like the user to be able to choose other as an option and a text box would appear, allowing them to type in the name of the machine. Can anybody help please?
  • Comment on How do I display a textbox only if a certain value is selected from a dropdown list
  • Download Code

Replies are listed 'Best First'.
Re: How do I display a textbox only if a certain value is selected from a dropdown list
by lostjimmy (Chaplain) on Aug 19, 2009 at 15:53 UTC
    This is a javascript question, not a perl question.
      I'm using the perl CGI module, I'm not using javascript anywhere.
        hi,

        I think what lostjimmy meant is that the solution is a javascript one, ie. you're going to have to use javascript. A quick and dirty example of how to go about it.

        <html> <head> <script language="javascript"> function getOther(selector,inputID){ var selector=document.getElementById(selector); var inputID=document.getElementById(inputID); if (selector[selector.selectedIndex].value=="Other"){ inputID.style.display= "block"; }else{ inputID.style.display="none"; } } </script> </head> <body> <select id="OSSelection" name = "OS" onchange="getOther('OSSelection', +'OtherOS')"> <option value = "Unix">Unix</option> <option value = "Multics">Multics</option> <option value = "VMS">VMS</option> <option value = "OSX">OSX</option> <option value = "Other">Other</option> </select> <input type="text" name="OtherOS" id="OtherOS" style="display: none;"> </body> </html>
        You could of course have a javascript library on your server which was included in each page and provided functions such as that above throughout the site.
        use CGI qw(:standard); ... print start_html({ "-title"=>"A JS Demo", "-script" => { "-src" =>"/scripts/functions.js" } } )
Re: How do I display a textbox only if a certain value is selected from a dropdown list
by sanku (Beadle) on Aug 21, 2009 at 11:07 UTC
    hi, Try out the following code
    print "Content-type:text/html\n\n"; print << 'JavaScript'; <script language='javascript'> function alertselected(selectobj){ ss=selectobj.selectedIndex if(ss == 2){ document.getElementById('new').style.display='block'; } else{ document.getElementById('new').style.display='none'; } } </script> JavaScript print "<html><body><form name=frm1>"; print "<select name=os onChange='alertselected(this)'> <option value='redhat'>Redhat</option> <option value='centos'>Centos</option> <option value='others'>Others</option> </select>"; print "<input id=new type=text name=newos style='display: none;'></for +m></body></html>";