Perl & CGI question: how to get user data and process it in one perl? (no html file!)

Mizugori

Senior member
May 3, 2007
496
0
0
I have been learning to make a basic html form, and then have it use the GET method to get the user's input data and pass it to a perl script (.pl) which then does something with the info, and outputs the result on the screen.

however, now I am trying to do this less messily, and I know I remember hearing that this can be done all in one perl file without the html file, but I do not know how... can anyone explain this to me, or point me in the right direction?? thanks!!
 

BitByBit

Senior member
Jan 2, 2005
474
2
81
My only server side scripting experience is with PHP, but the principles must surely be similar. If I've understood your post, you want to include the HTML and Perl script inside one text file, without having to link to an external file.
With PHP, HTML can be included in the PHP file before and after the <?php and ?> tags.
To submit data from a form for processing by the same file from which it originates, the address of the file is used in the 'action' attribute of the <form> element, so we end up with this: <form action="<?php echo $_SERVER['PHP_SELF'] ?>">. When the form is submitted, the page should reload and the variables will be set, and not null. You should have if statements to test the variables, and echo the results of your processing on screen if they have been.
 

Mizugori

Senior member
May 3, 2007
496
0
0
ok here is my code so far:

it displays the phrase "The word entered was:" right from the start when the page loads, without them even pushing the submit button. why is that? i was trying to make it be so that when the page loads there is a simple form, and then if they enter data it processes it... i'm still a bit confused.

Code:
#!/usr/bin/perl

use strict;
use CGI;

print "Content-type: text/html\n\n";

print '<FORM ACTION="http://blabla/blabla/filename.pl" METHOD=GET>';
print '<br>';
print '<hr>';

print 'Please enter a word: <INPUT TYPE="text" NAME="nombre">';
print '<INPUT TYPE="submit" VALUE="Submit">';

print '<br>';
print '<hr>';

my $c = new CGI;
my $nombre;

if ("submit")
        {
        $nombre = $c->param('nombre');
        print "The word entered was: $nombre";
        }
else
        {
        print "Invalid entry!";
        }
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
The problem with your code is that if ("submit") line. "Submit" is a string, which always evaluates to true (unless it's empty). You'll need to check a GET var to see if you should display that stuff.

This should work:

#!/usr/bin/perl

use strict;
use CGI;

my $c = new CGI;

# I like taking in the params as a hash
my %params = $c->Vars;

# Easier than typing that Content-type crap all the time
print $c->header;

# Simpler syntax here
print <<ENDFORM;
<FORM ACTION="http://blabla/blabla/filename.pl" METHOD=GET>
<br>
<hr>
Please enter a word: <INPUT TYPE="text" NAME="nombre">
<INPUT NAME="submit" TYPE="submit" VALUE="Submit">
<br>
<hr>
ENDFORM

# Could add a && $params{'nombre'} to the if check to make sure something was entered
if ($params{'submit'})
{
# Don't really need to do this first line here (could just put that hash in the string), but whatever
my $nombre = $params{'nombre'};
print "The word entered was: $nombre";
}
else
{
print "Invalid entry!";
}


Edited to fix some bugs.

Basically the only substantial changes I made where: I named your submit button "submit", then I checked if "submit" was set in the CGI vars (I did it in the hash %params). Other things were just stylistic changes to what I prefer.

Another thing: If you don't want to show the form once something has been submitted, you just need to put a if (!$params{'submit'}) around the form code (or stick it in that else statement).