Perl: Variable input via HTML


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Variable input via HTML
# 1  
Old 02-11-2005
Perl: Variable input via HTML

I am completely new to perl and am just going over the tutorials right now. What I am trying to attempt is to take the input from the HTML (in a form) and use those variables in a perl script. I've looked everywhere for a simple example on how to do this and cannot find it or do not understand what they mean. Here is what I have below:

#!/bin/perl
print "Content-type: text/html\n\n";

print <<ENDHTML;
<HTML>
<HEAD>
<TITLE>CGI Test</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="../cgi-bin/mycgi.pl">
name: <INPUT TYPE=TEXT NAME="realname"><BR>
email: <INPUT TYPE=TEXT NAME="email"><BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

ENDHTML
print "My name is: $realname\n";
print "My email is: $email\n";

Thanks in advance for any suggestions.
# 2  
Old 02-11-2005
You have not mentioned what are the problems you have for this example.

By the way , what webserver are you using ?

Last edited by bhargav; 02-13-2005 at 03:05 AM..
# 3  
Old 02-12-2005
First, make sure you have a proper shebang for perl. /bin/perl? I rarely see perl installed in /bin, but if it's indeed there then forget it.

Modern perl scripters no longer craft hacks to read form data from standard input primitively. In those code, POST form data used to be read from <STDIN> according to CGI specification, from which the content in the form of &-separated key=value pairs are usually extracted with a combination of split() and regular expressions. You may still find examples with code like this. However, this is prone to errors and is no longer recommended. The Perl community has agreed on a common standard - the CGI module. If your tutorial doesn't mention the CGI module, it should be considered out of date already and you ought to cast some questioning glances on it.

Here I modified your script a little bit to put in the missing parts. The CGI module knows how to find out whether a POST or GET form is involved, and then read and extract form data with the appropriate method for you. No fiddling of those dirty things required anymore:

Code:
#!/usr/bin/perl

use CGI;

my $cgi = new CGI;
print $cgi->header('text/html');

if (defined $cgi->param('ok')) {
 my %params = $cgi->Vars;
 print "My name is: ${params{realname}}<br>";
 print "My email is: ${params{email}}<br>";
} else {
print <<ENDHTML;
<HTML>
<HEAD>
<TITLE>CGI Test</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="mycgi.pl">
name: <INPUT TYPE=TEXT NAME="realname"><BR>
email: <INPUT TYPE=TEXT NAME="email"><BR>
<INPUT TYPE=SUBMIT NAME="ok">
</FORM>
</BODY>
</HTML>

ENDHTML
}

Some perl people actually recommend you to use the CGI object exclusively to output the form completely with its API rather than hardcoding the HTML tags in print() statements, but I think this is just a matter of taste. Isolating form snippet in templates and construct form output dynamically with a template engine (such as Template Toolkit) is seen as an even better approach.

Just put the script (mycgi.pl) in your Web server, give it executable chmod, and that should work. It should work if you have a reasonably recent version of CGI. It should be, if your Perl version is recent enough. The CGI module is shipped with Perl by default.

Last edited by cbkihong; 02-12-2005 at 11:00 PM.. Reason: use $cgi->header() instead of legacy way
# 4  
Old 02-13-2005
... and cbkihong should know, he wrote an extensive, 241 page book on PERL!

cbkihong's PERL book

Great job!
# 5  
Old 02-13-2005
Kudos cbkihong! I just downloaded a copy of the pdf.
# 6  
Old 02-13-2005
Great Job! I've often been impressed by cbkihong's perl expertise. But I want to point out that the book is available on a mirror site. cbkihong's web site is being hammered by downloads, so please consider using the mirror. Also note that cbkihong is looking for other mirror sites for his book. Let him know if you can help. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

2. Shell Programming and Scripting

Input data of a file from perl into HTML table

Hi , I need an help in perl scripting. I have an perl script written and i have an for loop in that ,where as it writes some data to a file and it has details like below. cat out.txt This is the first line this is the second line. .....Now, this file needs to be send in mail in HTML... (2 Replies)
Discussion started by: scott_cog
2 Replies

3. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

4. UNIX for Dummies Questions & Answers

HTML: Display contents of file using Variable

<tr><td bgcolor=#D7EBAD><b>Instructions :</b></td> <td>`cat temp.txt`</td></tr> Hi Experts, I have an requirement of displaying file contents in HTML mail , for which am using the above approach, Where as the output is kind of not as expected. If there are 5 lines in the file, in the... (4 Replies)
Discussion started by: scott_cog
4 Replies

5. Shell Programming and Scripting

Replacing variable values in html tags

Hi please help me with this . I have a file test.txt with following content $cat test.txt <td>$test</td> <h2>$test2</h2> and I have a ksh with following content $cat test.ksh #!/bin/ksh test=3 test2=4 while read line do echo $line done < test.html I am expecting the output as (4 Replies)
Discussion started by: panduandpavan
4 Replies

6. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

7. Shell Programming and Scripting

Perl + Korn + HTML

I have a perl script that prints up the html code and executes a few korn scripts to populate the web code. Disclaimer === I can throw together some korn scripts pretty quick. This code changes pretty frequently. I don't know enough about perl to do everything I need. One day maybe I'll get... (4 Replies)
Discussion started by: i9300
4 Replies

8. Shell Programming and Scripting

Pulling Input from HTML into Shell

I am trying to add to a Web page that I have, the ability to have users make requests as long as they key in a password. Can anyone help me with validating the text from the text field on the Web page into UNIX? Please let me know if I need to be clearer. Thanks, (1 Reply)
Discussion started by: mosammey
1 Replies

9. Shell Programming and Scripting

HTML parsing by PERL

i have a HTML report file..its in attachment(a part of the whole report is attached..name "input html.doc").also its source is attached in "report source code.txt" i just want to seperate the datas like in first line it should be.. NHTEST-3848498958-NHTEST-10.2-no-baloo a and so on for whole... (3 Replies)
Discussion started by: avik1983
3 Replies

10. Shell Programming and Scripting

perl and html

hi. im very new to perl. is it possible to fill up a web form and submit it using perl? example, i would like to sign up for a yahoo account though a perl script (ofcourse, granting the "type the characters as shown in imgage" is absent)? (8 Replies)
Discussion started by: marcpascual
8 Replies
Login or Register to Ask a Question