By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

FORM processing .cgi script - can someone check this?

100+
P: 135
I have a FORM with allows user to input their name and then user clicks submit which this goes to my .cgi script. Can someone check this .cgi script as the results are not being displayed as expected.

My post_it.cgi:

Expand|Select|Wrap|Line Numbers
  1. #!C:/perl/bin/perl.exe
  2.  
  3. print "Content-type:text/html\n\n";
  4.  
  5. @values = split(/&/,$ENV{'QUERY_STRING'});
  6. foreach $i(@values) {
  7.     ($varname, $mydata)=split(/=/,$i); 
  8.     print "$varname = $mydata\n";
  9. }
  10.  
  11. print "<html><head><title>FORM OUTPUT</title></head><body>";
  12. print "<h2>Results from FORM post</h2>\n";
  13.  
  14. foreach $key (keys(%FORM)) {
  15.     print "$key = $FORM{$key}<br>";
  16. }
  17.  
  18. print "</body></html>";
My .html document:

[HTML]<html><head><title>Post.html</title></head>
<body>

<form action="/cgi-bin/post_it.cgi" method="GET">
<pre>
Your Name: <input type="text" name="name">
</pre>
<input type="submit" value="Send">
</form>

</body>
</html>[/HTML]
Sep 13 '07 #1
Share this Question
Share on Google+
5 Replies


Kelicula
Expert 100+
P: 176
For one the split function returns an array, not scalar values.
You have never declared the %FORM hash before you try to populate it.

Also you may need to escape the equal sign in your split function line 7.

Like this:

Expand|Select|Wrap|Line Numbers
  1.  ($varname, $mydata)=split(/\=/,$i);
  2.  
But overall I would abandon the whole concept, and use the CGI module which allows you to easily access the input from a form.

Like this:

Expand|Select|Wrap|Line Numbers
  1. use CGI qw(:standard);
  2.  
  3. $name = param('name');
Now the variable name will equal whatever the user typed in the "name" input.

In fact, no matter how many inputs there are in the html page you can access them like that.
Let's say there are three: name, age, hometown

html:

[HTML]<html><head><title>The form</title></head>
<body>

<form action="thescript.pl" method="GET">

<input name="name" type="text" />
<input name="age" type="text" />
<input name="home" type="text" />

<input type="submit" value="Enter" />
</form>
</body>
</html>[/HTML]

Now in your script, you could do these:

Expand|Select|Wrap|Line Numbers
  1. use CGI qw(:standard);
  2.  
  3. $name = param('name');
  4. $age = param('age');
  5. $home = param('home');
  6.  
  7. print "Welcome ".$name." from ".$home." I see you are \n".$age;
  8.  
  9. # Or ....
  10.  
  11. @anArray = ($name, $age, $home);
  12.  
  13. print "@anArray";
  14.  
  15. # or whatever...
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
Hope it helps...
Sep 13 '07 #2

100+
P: 135
thank you for that works well, use cgi makes life easy.
Sep 14 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
For one the split function returns an array, not scalar values.
arrays are lists of strings/scalars so using the split() function to return a list of scalars is perfectly acceptable and quite common:

($varname, $mydata)=split(/=/,$i);

like most things in perl, context is important. split() can return a value in list or scalar context. List context would return an array or a list of scalars, scalar context returns how many fields are found but also populates the system array @_.

Expand|Select|Wrap|Line Numbers
  1. my $foo = 'this is a test';
  2. $n = split(/ /,$foo);
  3. print $n,"\n";
  4. print "$_\n" for @_;
But you should not make a habit (or even an occasional use) out of writing perl code like this. You should always return the values of split() to an array or list so as not to clobber the system array.
Sep 14 '07 #4

Kelicula
Expert 100+
P: 176
arrays are lists of strings/scalars so using the split() function to return a list of scalars is perfectly acceptable and quite common:

($varname, $mydata)=split(/=/,$i);

like most things in perl, context is important. split() can return a value in list or scalar context. List context would return an array or a list of scalars, scalar context returns how many fields are found but also populates the system array @_.

Expand|Select|Wrap|Line Numbers
  1. my $foo = 'this is a test';
  2. $n = split(/ /,$foo);
  3. print $n,"\n";
  4. print "$_\n" for @_;
But you should not make a habit (or even an occasional use) out of writing perl code like this. You should always return the values of split() to an array or list so as not to clobber the system array.



I was not aware of that. Thanks!
Sep 14 '07 #5

KevinADC
Expert 2.5K+
P: 4,059
I was not aware of that. Thanks!
You can familiarize yourself with the builtin perl functions:

http://perldoc.perl.org/index-functions.html

all the documentation also comes with perl so if you have perl installed locally you can read it on your local PC. If you have activestate it's all well formatted as html and readable in your browser. If you have Linux or other it is still included but you may have to read it directly from the man pages:

perldoc sort
Sep 14 '07 #6

Post your reply

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