www.createasite.net
freewh.890m.com

CGI Scripts
to Create a Website with Interactive Features

Get Form Input and Save to a File

There are 2 scripts in this lesson. The first script will display a form which will take input from the user. The second script processes the information from the form and saves it in a file and returns a message to the user.

form_script.pl

This script asks the user to enter a first and last name in separate text boxes. Save this script in the cgi-bin\ directory. It could also have been written as an HTML document and saved in the htdocs\ directory. (Note: For html change action to action="cgi-bin/save_file.pl") Notice the action attribute of the form calls for a script named save_file.pl

#!/usr/bin/perl
#form_script.pl
print "Content-type:text/html\n\n"; #Content Header
print "<html>\n";
print "<head><title>Sample Form</title></head>\n";
print "<body>\n";
print "<form method="POST" action="save_file.pl">\n";
print "<pre>\n";
print "Enter First Name:<input type="text" name="first" size="20">\n";
print "Enter Last Name:<input type="text" name="last" size="20">\n";
print "<input type="submit" value="Submit"><input type="reset">\n";
print "</pre>\n";
print "</form>\n";
print "</body>\n";
print "</html>\n";


Test this Script



save_file.pl

This script will take the information entered on the form, parse it, save it in a text file in the cgi-bin directory and return a message to the user.

#!/usr/bin/perl
#save_file.pl
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /;
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$first=$formdata{'first'};
$last=$formdata{'last'};

open(INFO, ">>names_file.txt"); # Open for appending
print INFO "$last|$first\n";
close (INFO);


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

print "<html>\n";
print "<head><title>Sample Response Page</title></head>\n";
print "<body>\n";
print "Thank you: $first $last\n";
print "</body>\n";
print "</html>\n";

More Instructions

After you have copied both scripts to the cgi-bin directory, load form_script.pl into the editor while running on your server. Enter a first and last name and press the Submit button. Add several names so that you can use the file in the remainder of the exercises. Using the text file option on the editor look for the file names_file.txt that is created in the cgi-bin directory. Notice that the last and first names are saved on the same line separated by the pipe symbol. The next script will show you how to open the file, remove the pipe symbol and display the contents.

To modify the fields of the database to meet your needs you would add the text input boxes to your form.
Then add a line of code similar to: $last=$formdata{'last'};
for the name attribute of each text box. Example:$street=$formdata{'street'};
Then change the line that saves the data to:
print INFO "$last|$first|$street\n";
Each of the scripts in the following exercises would have to be changed accordingly.
Split statements would be changed to:
($last,$first,$street) = split(/\|/,$line);

Note: If the scripts don't work:
1...Check the shebang line and make sure it is the path recommended by your web host to access the perl interpreter on your server.
2...Be sure to use the Chmod function on your ftp client or file manager to change the permissions of the script to 755


cgi script #1

Create a Website with CMS
Member Registration and Polls Automatic

Member Registration and Polls are automatic using CMS.

Visit our Joomla Website , then get your CMS at CREATEaSITE.BIZ for only $4.25 with Free Setup and Free Domain Name.

Just want to pay more? Get it an one of these leading web hosts:
HostMonster    Enter for a chance to win a new car!      

cgi script #3

Net Success 2000 Plus Inc
PO Box 1508
Somerset, KY 42502
Copyright 2000 - 2007 Net Success 2000 Plus Inc
Last Modified: January 15, 2008

|HTML OnLine | Website Tutorial | Home |