473,386 Members | 1,799 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,386 software developers and data experts.

FORM processing .cgi script - can someone check this?

135 100+
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
5 2024
Kelicula
176 Expert 100+
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
patelxxx
135 100+
thank you for that works well, use cgi makes life easy.
Sep 14 '07 #3
KevinADC
4,059 Expert 2GB
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
176 Expert 100+
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
4,059 Expert 2GB
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

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

Similar topics

8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
4
by: Howard Jess | last post by:
In Opera 8.01 (Linux; Build 1204) and in Opera 7.54 (Windows XP; Build 3865), my form disappears from the HTML markup (below). To summarize: 1) In a <script> block in the <head> I create a form...
3
by: raj chahal | last post by:
Hi there i have created a registration page containing a form than sends username password to an asp processing page. If the user exists it sends the user back to the registration page with...
27
by: Scott | last post by:
I've been trying to come up with a way to ensure user input is coming from the form on my site, and not auto-submitted from elsewhere, and I don't want to use the "enter the code shown in the...
27
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it...
7
ak1dnar
by: ak1dnar | last post by:
Hi, I got this scripts from this URL There is Error when i submit the form. Line: 54 Error: 'document.getElementbyID(....)' is null or not an object What is this error. Complete Files
22
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
2
by: Ronald Raygun | last post by:
I have a form, which contains a textfield for a user to enter their username. I want to provide a button that allows the user to check to see whether that username has already been taken up by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.