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

drop down menus with CGI/Perl

by Anonymous Monk
on May 22, 2009 at 06:18 UTC ( #765613=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to create a simple drop down menu in a CGI/Perl script that allows the user to choose how they would like their search results to be sorted.

All the tutorials and bits of code I've come across don't seem to do anything at all. And by that i mean you can see the menu and click an option but no information is sent.

I'd preferably like this drop down menu to not have a submit button either, but to submit as soon as an option is selected from the menu. And nothing i've come across does anything like that either, in perl/CGI anyway.

I'm getting really frustrated and confused. Please help!

Comment on drop down menus with CGI/Perl
Re: drop down menus with CGI/Perl
by davido (Archbishop) on May 22, 2009 at 07:11 UTC

    I'm not sure quite where you are from the standpoint of understanding your way around a CGI application, but the question leads me to believe that you're just starting to get your feet wet. That being the case, I hope the following suggestions get you started in the right direction:

    • CGI stands for Common Gateway Interface. It's a set of tools that can be manipulated by an application to generate dynamic HTML that gets presented to the browser. CGI is programming language independent; a CGI application could be programmed in Perl, or C, or most other server-side languages. We here tend to favor Perl for various reasons, but YMMV.
    • The CGI application (in this case in the form of a Perl script) generally does two things. First, it generates HTML. Second, it processes GET and POST input from forms. "Processes" is a terse way of saying it reads the input, decides what to do with it, does what it's supposed to do with the input, and then responds to the results of said processing by outputting more HTML to the browser.
    • A CGI application doesn't necessarily HAVE to output a lot of HTML. A form could be hard-coded HTML, and the HTML that gets sent out in response to the form input could also be hard-coded HTML from a file or templating system. But that's a little more advanced topic.
    • It is prudent to use CGI. You shouldn't be trying to read and deal with the low level stuff required to safely read form data. CGI.pm can also output HTML, though there are usually easier ways to accomplish that via a templating package.
    • A drop down menu is a HTML entity that exists within a form. The script still has to know what to do with the form data.
    • Forms don't respond immediately to the selection of a drop-down item without the user clicking "submit" unless there is some client-side programming too, for example, with Javascript or one of the newer client-side flavors of the day.
    • If you're interested in learning to program, and gaining an understanding of the topics I touched on here, get a decend CGI programming book, such as the Mouse book (CGI Programming with Perl, I believe it's called, by O'Reilly & Assoc.). A later edition is preferable as much has changed over the years. Also reading CGI is very important. You can't just guess at all this and hope it will work out.

    I hope some of this helps. If programming turns out to not be something you're interested in, you're better off finding someone who is interested in it, because the learning curve will quickly put you off if you're just looking for a quick solution. But if you have a desire to learn this stuff, stick around and enjoy. Just don't feel bad when it takes some work learning the art.


    Dave

      daivdo's response above is good advice, what you appear to want to do is to make change dynamic, this could be done with an AJAX XHtmlRequest, and a rewrite of the results div done in Javascript called by an onchange event handler associated with the drop down menu.

      However to get a feel for the idea, a simple

      <!-- <script language="javascript"> function sortResults(){ var myForm=getElementByID("myForm"); myForm.submit(); } </script> --> <form action="returnResults.pl" id="myForm" method="POST"> <select name="sortBy"> <option value="byDate">Date...</select> <input type="text" name="searchCriteria" value="$WHATEVER WE GOT INITI +ALLY"> </form>
      This will recall the script with a sortBy parameter set, of course it will reload the whole page.

      After you get this method to work, have a look at AJAX for inline changes on a page, again as davido said, knowledge has to be acquired and it's not an instantaneous process, however it's a rewarding one.

      Happy learning ;)

Re: drop down menus with CGI/Perl
by jhourcle (Prior) on May 22, 2009 at 17:22 UTC
    I'd preferably like this drop down menu to not have a submit button either, but to submit as soon as an option is selected from the menu

    This part has nothing to do what the server-side language is -- you have to do it in javascript. Attach an onChange event to the select element.

      Try this code :
      <P>Choose your destination :<BR><BR> <select name="select" onchange="document.location.href=this.options[th +is.selectedIndex].value;"> <option value="http://google.com">optionA</option> <option value="http://yahoo.com">optionB</option> <option value="http://msn.com">optionC</option> </select>
      Regds Vivek
        Try This code, it is using Java Script :
        <P>Choose your destination : <BR><BR> <select name="select" onchange="document.location.href=this.options[th + +is.selectedIndex].value;"> <option value="http://google.com">optionA</option> <option value="http://yahoo.com">optionB</option> <option value="http://msn.com">optionC</option> </select>
        Regds
        Vivek

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-12-26 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls