Click to See Complete Forum and Search --> : Perl Question
MrNewbie
02-20-2001, 10:01 AM
I'm REALLY new to perl and I know this is probably a really basic quesion but somehow I'm having a problem finding out the answer.
I'm submitting a form like this:
<html>
<head>
<title>Perl Page</title>
</head>
<body>
<form action="/cgi-bin/myscript.pl">
<p>Name: <input name ="name">
<p>Age: <input name ="age">
<p><input type="submit" value="Submit">
</form>
</body>
</html>
I'm getting the form fields like this:
use strict;
use CGI qw(:standard);
$name = param('name');
$age = param('age');
But if I write that data to a file, when someone else submits the form, how would I store their info in the file according to alphabetical order? This is probably a really simple question so sorry. Hehe.
MrNewbie
02-20-2001, 10:30 AM
Hmm, for some reason my script wont work at all :(
MrNewbie
02-20-2001, 01:21 PM
Can someone post how I'd get strings from form fields please?
Gotenks
02-20-2001, 03:44 PM
Originally posted by MrNewbie:
<html>
<head>
<title>Perl Page</title>
</head>
<body>
<form action="/cgi-bin/myscript.pl">
<p>Name: <input name ="name">
<p>Age: <input name ="age">
<p><input type="submit" value="Submit">
</form>
</body>
</html>
In order to get strings returned from that form you need to give them a type. Such as this...
<input name="name" type="text">
Gotenks.
MrNewbie
02-20-2001, 03:59 PM
Thanks I'll give that a shot if my crummy ftp server will let me connect. Btw the script I'm messing around with to try to get this kind of working so far is this:
#!/usr/bin/perl
use strict;
use CGI qw(:standard);
print 'Content-type: text/html';
print "<html><head><title>Test Perl</title></head><body>";
print "<h1>Hello, ", param('name'), "!\n";
print "you are ", param('age'), "</h1>;
print "</body></html>";
Can anyone see any problems with that?
MrNewbie
MrNewbie
02-20-2001, 04:02 PM
It didn't work :( what am I doing wrong?
TheLinuxDuck
02-20-2001, 05:02 PM
#!/usr/bin/perl
use strict;
use CGI qw(:standard);
print 'Content-type: text/html';
[/b]<HR></BLOCKQUOTE>
Make sure to include two newlines after the content display, as:
print "Content-type: text/html\n\n";
print "<html><head><title>Test Perl</title></head><body>";
print "<h1>Hello, ", param('name'), "!\n";
print "you are ", param('age'), "</h1>;
[/b]<HR></BLOCKQUOTE>
The way I learned to use the CGI was to create a new CGI item, via:
my($newCGI)=new CGI();
and then access the parameters as:
$newCGI->param()
IMHO, it's always a good idea to include error checking even for a simmple test script. You never know what will turn up. I would do it something like (for this simple test):
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI qw( :param);
my($newCGI)=new CGI();
print("Content-type: text/html\n\n<html><head><title>TEST</title></head>",
"<body><h1>Hello, ",
($newCGI->param("name")?$newCGI->param("name"):"nobody"),
"!<br>You are ",
($newCGI->param("age")?$newCGI->param("age"):"some odd"),
" years old.<br></h1></body></html>");
exit;
And, if you can, it's always a good idea to test a script out at the command line before running it. You can catch many errors and problems by doing so.
:)
[ 20 February 2001: Message edited by: TheLinuxDuck ]
Ben Briggs
02-20-2001, 05:13 PM
In the form tag, I'm pretty sure you have to put in a METHOD extention with either GET or POST:
<html>
<head>
<title>
Hello
</title>
</head>
<body>
<form action='myscript.pl' method='GET'>
...
</form>
</body>
</html>
lazy_cod3R
02-20-2001, 05:20 PM
well i think you have an error with the form, you didnt give it what method it should submit on ie post or get and the other thing is i think for the input of name and age u have to do
<input type=text name=name> and tell it that its a text type if what you are after is a text field.
u also need a parser of some sort to deal with the inputs, there are alot of free ones out there
TheLinuxDuck
02-20-2001, 05:23 PM
After doing some CGI's, I've got a trick I use for making the scripts easier to work with. These are what I have found to work well for me, and may not always work the best for all situations, but I think they work wonders. :)
1. Use a buffer for output, instead of immediately dumping html code to the stdout. This will allow you to incorporate error checking and error displaying throughout the code more easily than ever having to wonder at what point through the CGI you've dumped what data to the screen.
2. Create an errorMessage sub that takes in any number of scalars, and can output a complete html doc on it's own, as well as terminating the script:
sub errorMessage($;@)
{
print "Content-type: text/html\n\n<html><head><title>ERROR</title></head>",
"<body><h1>ERROR!</h1><br>@_</body></html>";
}
exit;
These two things together will give you some control over how the CGI works. An example would be with your example. Let's say that we don't want the CGI to default to a specific name or age, but we want to have an error check for these params. If they don't exist, we exit with an error, otherwise, we dump the buffered data to the screen:
#!/usr/bin/perl -w
use strict;
use warnings;
use CGI qw(:param); # We only really need param
#
# Declare the sub so that we must pass at least 1 scalar to it
#
sub errorMessage($;@);
#
# Init our CGI object
#
my($newCGI)=new CGI();
#
# Begin our buffer.
#
my($buffer)="Content-type: text/html\n\n<html><title>TEST</title></head>".
"<body><h1>Hello, ";
#
# Loop through the parameters we require and error if even just one isn't
# found.
#
for(qw(name age)) {
errorMessage("Did not receive the parameter $_") if(!$newCGI->param($_));
}
#
# Finish our buffer, since we know that the params we need are there
#
$buffer.=$newCGI->param("name")."!<br>You are ".$newCGI->param("age").
" years old.<br></h1></body></html>";
print $buffer;
exit;
#
# This is a VERY handy function. $;@ means that the sub must take in at
# least 1 scalar, but can take many, including an array.
#
sub errorMessage($;@)
{
print "Content-type: text/html\n\n<html><head><title>ERROR</title></head>",
"<body><h1>ERROR!</h1><br>@_</body></html>";
exit;
}
TheLinuxDuck
02-20-2001, 05:26 PM
Originally posted by Ben Briggs:
In the form tag, I'm pretty sure you have to put in a METHOD extention with either GET or POST:
If I'm not mistaken, the method is implied as get unless otherwise stated. I could be wrong. :)
MrNewbie
02-20-2001, 05:57 PM
Wow thanks TLD you did it again! And thanks everyone else. I'll give that code a try and hopefully should be able to modify it to what I need.
And yeah I think your right I think it assumes get unless otherwise stated just as doing something like this:
<input name ="age">
Assumes a text box because its not specifically specified. (At least in IE).
Thanks again. :)
Ben Briggs
02-21-2001, 01:33 PM
Originally posted by TheLinuxDuck:
If I'm not mistaken, the method is implied as get unless otherwise stated. I could be wrong. :)
Let's not kid ourselves... it's more likely that I'm wrong! :D
I thought I'd mention that some servers require the cgi script have an extension .cgi, instead of .pl (so you have to have the #! /usr/../perl as the first line).
And make sure all of your file permissions are set for all files and directories you're using.
What's the difference between post and get?