Using Form with Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yjulien
    New Member
    • Feb 2011
    • 43

    Using Form with Perl

    Hello,

    I have a new project. I created a Excel file that calculate Solar Power requirement base on someone location and electric consumption. It works nicely.

    Now I want to make a HTML model inspired from this file.

    The Insolation information would be stored in a separate file. The user will have to provide me with it's city for the calculation to commence.

    I don't know how to use the <FORM> tag in Perl to accept input from users. The first thing the user need to input is the city. I don't know what to put for ACTION ?

    Code:
    print qq~<form method="POST" [I]action="????"[/I]>
      <select size="1" name="Cities">
      <option>Name</option>
      <option>Montreal</option>
      <option>Toronto</option>
      </select><input type="submit" value="Submit" name="B1">
      <input type="reset" value="Reset" name="B2"></p>
    </form>~;
    Questions : What is the value for ACTION? I want the form to activate a Sub routine in the script. Lets call it "Search_City_in formation".

    Where will be store the value (the city's name)? Is it in the variable $Cities from the select drop list?

    Thanks for your help,

    Yves
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    The action attribute needs to point to your form processing script.

    The script will parse the form submission fields and execute the required sub based on the values passed in the form submission.

    Comment

    • yjulien
      New Member
      • Feb 2011
      • 43

      #3
      Hi Ron,

      The sub routine is in the same script. How do I actually call it? I tried the usual method, i.e. action = "&Sub_Name; " but it does not work.

      Any idea ?

      Thanks,

      Yves

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        Where did you get the idea that
        action = "&Sub_Name; "
        is "the usual way"?

        The action attribute expects/requires a url, not a perl subroutine call. The url could be either a relative or absolute url.
        W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


        If you leave out the action clause, then it defaults to sending the form data to the same script that generated the form i.e., itself.

        The script would need to parse the form submission and then execute the Search_City_inf ormation subroutine passing it the city retrieved from the form submission.

        Assuming the form processing script is called CitySearch.pl, the action attribute would be:
        <form method="POST" action="cgi-bin/CitySearch.pl">
        and if it is using the CGI module to parse the form submission, you could do something like this:
        Code:
        my $cgi = CGI->new;
        my $city = $cgi->param('Cities');
        Search_City_information($city);
        Last edited by RonB; Dec 20 '13, 03:09 PM.

        Comment

        • yjulien
          New Member
          • Feb 2011
          • 43

          #5
          Hi Ron,

          By the time I read your response, I had figure out how to use the ACTION command. However I still can't pass on the values of the input boxes.

          Is this code you gave me how I should do it?

          Code:
              my $cgi = CGI->new;
              my $city = $cgi->param('Cities');
              Search_City_information($city);
          > Where did you get the idea that
          > action = "&Sub_Name; "
          > is "the usual way"?

          Well I always used a command like this to activate a sub routine

          Code:
          require  "$path/library/CitySearch.pl";
            &Sub_Name;
          So I assumed the &Sub_Name; is the usual command...

          However I'm no expert, as you already realized... ;-)

          I will try the code you gave me above and let you know.

          Thanks,

          Yves
          Last edited by zmbd; Dec 22 '13, 05:47 AM. Reason: [Last edited by yjulien; 9 Hours Ago at 02:28 PM. Reason: spelling][z{red=colour read=verb (^_^) fixed}]

          Comment

          • yjulien
            New Member
            • Feb 2011
            • 43

            #6
            Hi Ron,

            I must be dumb! I still can make it work. I don't know how to activate the sub routine and pass on the input from the form. I wrote a very small program. Here is the very small program I wrote without success. Can you help?

            Code:
            #!/usr/local/bin/perl -w
            
            print "Content-type: text/html\n\n";
            
            print qq~<form method="post">
                    Where do you live ?:
                   <select size=1 name=Cities>
                   <option>Chose one!</option>
                   <option>Montreal</option>
                   <option>New York</option>
                   <option>Miami</option>
                    </select><p>
            
                   <input type=submit value=Submit name=City>
                   </form><p>~;
            
            
            sub   City {
            
            print qq~ You live at $Cities~;
            
            }
            
            exit;

            What is missing in my code?

            Thanks,

            Yves

            Comment

            • RonB
              Recognized Expert Contributor
              • Jun 2009
              • 589

              #7
              Every Perl script you write should load the following 2 pragmas.
              Code:
              use warnings;
              use strict;
              You can remove the -w switch since the warnings pragma superceeds it.

              Next, you'll need to load the CGI module to parse the form submission.
              Code:
              use CGI;
              The parsing of the form submission and executing your sub could be done either before or after printing the actual form since it (the subroutine execution) will only be done if the form was submitted.

              There are several other improvements I normally suggest, but here's the resulting script with the minimal amount of changes.
              Code:
              #!/usr/local/bin/perl
              
              use strict;
              use warnings;
              use CGI;
              
              my $cgi  = CGI->new;
              my $city = $cgi->param('Cities');
              
              print "Content-type: text/html\n\n";
               
              print qq~<form method="post">
                      Where do you live ?:
                     <select size=1 name=Cities>
                     <option>Chose one!</option>
                     <option>Montreal</option>
                     <option>New York</option>
                     <option>Miami</option>
                      </select><p>
               
                     <input type=submit value=Submit name=City>
                     </form><p>~;
              
              if ($city) {
                  City();
              }
               
              sub City {
              
                  print qq~ You live in $city~;
              
              }
               
              exit;

              Comment

              • yjulien
                New Member
                • Feb 2011
                • 43

                #8
                This is great ! Thanks RON !

                I need to understand something. What is the use of the if condition?

                Code:
                 if ($city) {
                    City();
                }
                How does it work? It check the value of the drop list over what and then call the sub? Can you explain to me how it actually work?

                Thanks again,

                Yves

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  The script gets executed twice; once when the user loads the page and the second time when they submit the form. The first time it only outputs the form and the second time it outputs the form and the value selected by the user when submitting the form.

                  Since $city won't have a value until the second execution, you need to test $city to make sure it has a value before executing your subroutine.

                  Comment

                  • yjulien
                    New Member
                    • Feb 2011
                    • 43

                    #10
                    So if $city has no value, the Sub is not call. If it has any value, the the Sub is call. Ok It's clear now.

                    Let me experiment with all this. I'm sure I will have more questions later... ;-)

                    Thanks you Ron,

                    Yves

                    Comment

                    • yjulien
                      New Member
                      • Feb 2011
                      • 43

                      #11
                      Hi Ron,

                      I still need you help understanding the form.

                      I expanded the program a bit and included new field to input. Once I call the sub-routine, I do some calculation on these field. I also created new variable inside the sub to accumulate the result.

                      Normally (in other script I wrote), these variable will retain there value once the sub is finish but in this case, only the original input field will survive. All new variable I created loses there value upon existing the sub..

                      DO you know why?

                      Thank,

                      Yves

                      Comment

                      • RonB
                        Recognized Expert Contributor
                        • Jun 2009
                        • 589

                        #12
                        You declared those vars in too small of a scope. If you need them after the execution of the sub, then they need to be declared outside of the sub and prior to its execution.

                        Comment

                        • JamRoll
                          New Member
                          • Nov 2014
                          • 8

                          #13
                          the best way to do what you want is:

                          Yves!

                          in HTML code, each form element (input fields, drop downs, textareas, radio and checkboxes, etc) can (and ought to) have a name. eg: <input name='data'>

                          here it is simply done:

                          Perl Code (filename: testform.pl):
                          Code:
                          my $city = getParam("city"); # see below for description of this
                          if ($city) {
                            print "you live in $city!";
                          
                            # to write this to a text file:
                          
                            open F, ">data.txt"; # notice the comma!
                            print F "$city"; # notice NO comma!
                            close F;
                          } else {
                            print "<form action=\"testform.pl\">\n";
                            print "  <select name=city>\n";
                            print "    <option value=0>Abbotsford</option>\n"; # </option> is not required, but i like to add it in for clarity's sake.
                            print "    <option value=1>Burnaby</option>\n";
                            print "    <option value=2>Chilliwack</option>\n";
                            print "  </select>\n";
                            print "  <input type=submit value="Move to city">\n";
                            print "</form>\n";
                          }
                          getParam("city" ) is a simple sub I created which does all that "CGI->new" stuff you see. That way, I don't have to remember how to do it, and it makes life really simple.

                          also, URL parameters are case sensitive. CITY and city are two different params so make sure you get that right as well.

                          Comment

                          Working...