Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

perl CGI - processing a form

Status
Not open for further replies.
Jun 30, 2003
196
GB
I have a form on on my web site that collects the name, age, gender and email address of people. i want to create a perl cgi script that saves this data to a file.

Could anyone help. If you give me code could you comment it for me so i know what it is doing.

Do i have to save all the information to an array then print this array to a new line in a file?
 
I have not run this so it may have a blem or two, but hopefully it shows the general flow.
Code:
#!/usr/bin/perl
use strict;
use CGI;
use CGI qw('fatalsToBrowser');
use CGI wq('warningsToBrowser');

my $cgi_object = new CGI;
print $cgi_object->header, $cgi_object->start_html, '<p>';

# retrieve inputs
my $name     = $cgi_object->param('name');
my $age        = $cgi_object->param('age');
my $gender    = $cgi_object->param('gender');
my $email    = $cgi_object->param('email');

# if inputs look reasonable, open file and write data.
if (($name =~ /^\w+$/) &&
    ($age =~ /^\d+$/)&&
    ($gender /^male|female$/)&&
    ($email =~ /.+@\w+\.\w+)) # overly simple email regex
    {
    # you need to add a little file locking here
    open(DAT,&quot;>>data.txt&quot;) or 
        die &quot;Failed to open DAT file, $!\n&quot;;
    print DAT &quot;$name|$age|$gender|$email\n&quot;;
    close DAT;
    print &quot;Thank you for your submission.\n&quot;;
    }    
else {
    print &quot;Invalid inputs.  Please go back and try again.\n&quot;;
    }

print '</p>', $cgi_object->end_html;

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top