473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using Form with Perl

43
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 ?

Expand|Select|Wrap|Line Numbers
  1. print qq~<form method="POST" action="????">
  2.   <select size="1" name="Cities">
  3.   <option>Name</option>
  4.   <option>Montreal</option>
  5.   <option>Toronto</option>
  6.   </select><input type="submit" value="Submit" name="B1">
  7.   <input type="reset" value="Reset" name="B2"></p>
  8. </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_information".

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
Dec 19 '13 #1

✓ answered by RonB

Every Perl script you write should load the following 2 pragmas.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. 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.
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI;
  6.  
  7. my $cgi  = CGI->new;
  8. my $city = $cgi->param('Cities');
  9.  
  10. print "Content-type: text/html\n\n";
  11.  
  12. print qq~<form method="post">
  13.         Where do you live ?:
  14.        <select size=1 name=Cities>
  15.        <option>Chose one!</option>
  16.        <option>Montreal</option>
  17.        <option>New York</option>
  18.        <option>Miami</option>
  19.         </select><p>
  20.  
  21.        <input type=submit value=Submit name=City>
  22.        </form><p>~;
  23.  
  24. if ($city) {
  25.     City();
  26. }
  27.  
  28. sub City {
  29.  
  30.     print qq~ You live in $city~;
  31.  
  32. }
  33.  
  34. exit;
  35.  

12 2496
RonB
589 Expert Mod 512MB
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.
Dec 20 '13 #2
yjulien
43
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
Dec 20 '13 #3
RonB
589 Expert Mod 512MB
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.
http://www.w3schools.com/tags/att_form_action.asp

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_information 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:
Expand|Select|Wrap|Line Numbers
  1. my $cgi = CGI->new;
  2. my $city = $cgi->param('Cities');
  3. Search_City_information($city);
Dec 20 '13 #4
yjulien
43
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?

Expand|Select|Wrap|Line Numbers
  1.     my $cgi = CGI->new;
  2.     my $city = $cgi->param('Cities');
  3.     Search_City_information($city);
  4.  
> 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

Expand|Select|Wrap|Line Numbers
  1. require  "$path/library/CitySearch.pl";
  2.   &Sub_Name;              
  3.  
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
Dec 21 '13 #5
yjulien
43
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?

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl -w
  2.  
  3. print "Content-type: text/html\n\n";
  4.  
  5. print qq~<form method="post">
  6.         Where do you live ?:
  7.        <select size=1 name=Cities>
  8.        <option>Chose one!</option>
  9.        <option>Montreal</option>
  10.        <option>New York</option>
  11.        <option>Miami</option>
  12.         </select><p>
  13.  
  14.        <input type=submit value=Submit name=City>
  15.        </form><p>~;
  16.  
  17.  
  18. sub   City {
  19.  
  20. print qq~ You live at $Cities~;
  21.  
  22. }
  23.  
  24. exit;

What is missing in my code?

Thanks,

Yves
Dec 22 '13 #6
RonB
589 Expert Mod 512MB
Every Perl script you write should load the following 2 pragmas.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. 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.
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use CGI;
  6.  
  7. my $cgi  = CGI->new;
  8. my $city = $cgi->param('Cities');
  9.  
  10. print "Content-type: text/html\n\n";
  11.  
  12. print qq~<form method="post">
  13.         Where do you live ?:
  14.        <select size=1 name=Cities>
  15.        <option>Chose one!</option>
  16.        <option>Montreal</option>
  17.        <option>New York</option>
  18.        <option>Miami</option>
  19.         </select><p>
  20.  
  21.        <input type=submit value=Submit name=City>
  22.        </form><p>~;
  23.  
  24. if ($city) {
  25.     City();
  26. }
  27.  
  28. sub City {
  29.  
  30.     print qq~ You live in $city~;
  31.  
  32. }
  33.  
  34. exit;
  35.  
Dec 22 '13 #7
yjulien
43
This is great ! Thanks RON !

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

Expand|Select|Wrap|Line Numbers
  1.  if ($city) {
  2.     City();
  3. }
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
Dec 22 '13 #8
RonB
589 Expert Mod 512MB
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.
Dec 23 '13 #9
yjulien
43
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
Dec 23 '13 #10
yjulien
43
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
Dec 24 '13 #11
RonB
589 Expert Mod 512MB
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.
Dec 24 '13 #12
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):
Expand|Select|Wrap|Line Numbers
  1. my $city = getParam("city"); # see below for description of this
  2. if ($city) {
  3.   print "you live in $city!";
  4.  
  5.   # to write this to a text file:
  6.  
  7.   open F, ">data.txt"; # notice the comma!
  8.   print F "$city"; # notice NO comma!
  9.   close F;
  10. } else {
  11.   print "<form action=\"testform.pl\">\n";
  12.   print "  <select name=city>\n";
  13.   print "    <option value=0>Abbotsford</option>\n"; # </option> is not required, but i like to add it in for clarity's sake.
  14.   print "    <option value=1>Burnaby</option>\n";
  15.   print "    <option value=2>Chilliwack</option>\n";
  16.   print "  </select>\n";
  17.   print "  <input type=submit value="Move to city">\n";
  18.   print "</form>\n";
  19. }
  20.  
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.
Nov 1 '14 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: M Wells | last post by:
Hi All, Is it possible to use form criteria in a query / view in an Access 2003 ADP with SQL Server as the backend? ie something like: select * from mytable where recid = forms!! I'm...
1
by: JTrigger | last post by:
I need to call a .Net web service that takes an array or object as a parameter from a PERL client. I have been trying to use the PERL SOAP::Lite package to do this without success. I can call one...
2
by: David Bear | last post by:
almost 41 million hits from google about using perl mod's in python was a bit discouraging...So, I thought about asking humans. There are some perl modules written to manage files, acls, etc in...
4
by: Sofia | last post by:
Hi there is text field and a button.... now i have to send this textfield value to another page without using form tag......i have to use javescript... is there anyone who can help me regarding...
0
by: smritikapoor | last post by:
I am trying to connect to an https server using NNTP perl module. I have been able to connect to normal NNTP server using the NNTP perl module but cannot connect to an https server. Please help me...
3
by: 1965 | last post by:
Hi, All. I want to pass a value to asp file on server side but NOT using form post/get. For example: <input id="regionbox" type="hidden" name="region" value="abc"> function dothing ......
1
by: norazirah | last post by:
Currently i use three different version of Oracle developer for system development (Oracle 6i, Oracle 10g and old version, Oracle 4.5). There is no problem using Form Designer (Developer 2000) until...
1
by: jasper123 | last post by:
Hello, I am developing a cgi-perl script that takes some value from a html form as input and stores them in a data file. I have a program called "irr" in my server, it is executed just by typing irr...
5
oranoos3000
by: oranoos3000 | last post by:
hi I want to send value of textbox without using form tag and submit button to another page? Is it possible with php ? How do I get this thing work? thanks alot
1
by: Kamalendu | last post by:
1. I am using Padre perl IDE 2. I have DBD-Oracle-1.19 installed in my machine. 3. Oracle 10g Express Edition is Installed. Source code #!/usr/bin/perl -w use 5.006; use strict;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.