![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pulling Input from HTML into Shell | mosammey | Shell Programming and Scripting | 1 | 02-17-2008 06:02 PM |
| Using Perl to add hyperlink to html files | Raynon | Shell Programming and Scripting | 17 | 11-01-2007 01:03 AM |
| HTML parsing by PERL | avik1983 | Shell Programming and Scripting | 3 | 02-23-2007 05:25 AM |
| perl and html | marcpascual | Shell Programming and Scripting | 8 | 09-21-2005 04:47 PM |
| Passing FORM(HTML) variable to ksh | douknownam | Shell Programming and Scripting | 1 | 02-25-2005 12:50 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
|||
|
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
}
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 07:00 PM. Reason: use $cgi->header() instead of legacy way |
|
||||
|
... and cbkihong should know, he wrote an extensive, 241 page book on PERL!
cbkihong's PERL book Great job! |
|
||||
|
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!
|
||||
| Google The UNIX and Linux Forums |
| Tags |
| regex, regular expressions |
| Thread Tools | |
| Display Modes | |
|
|