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
|