CGI Scripts
to Create a Website with Interactive Features
Delete a Name From the Database
There are 2 scripts in this exercise. The first is a form that asks the user for a first and last name to delete from the database file, names_file.txt. The second script will process the form search for the matching names and delete the data.
search_to_delete.pl
This script produces a simple form that queries the user for a first and last name to delete from the database file.
#!/usr/bin/perl#search_to_delete.plprint "Content-type:text/html\n\n"; #Content Headerprint "<html>\n";print "<head><title>Search Form</title></head>\n";print "<body>\n";print "<form method=\"POST\" action=\"delete_name.pl\">\n";print "<pre>\n";print "Enter Last Name:<input type=\"text\" name=\"search1\" size=\"20\">\n";print "Enter First Name:<input type=\"text\" name=\"search2\" 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";delete_name.pl
This script parses the information from the form
opens names_file.txt * and searches for a match.
If a match is not found it calls a subroutine that returns a message to the user.
If a match is found it converts the database to a hash(associative array).
It then searches the hash and returns the key for the matching value,
deletes the key-value pair
rewrites the altered file, converting it back to a simple array of lines
reopens the altered file and displays it to the screen.
The script demonstrates the difference between writing and appending to a file. Uses new functions chomp, join, delete. Illustrates the use of hashes to manipulate data, subroutines and branching conditional statements.
* Note: If you haven't completed the previous exercises, names_file.txt is provided below.
#!/usr/bin/perl#delete_name.pl#parse the form dataread(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";}$search1=$formdata{'search1'};$search2=$formdata{'search2'};# Open db for reading and displayopen(INFO, "names_file.txt");@array=<INFO>;close (INFO);$count=0;$continue=0;#check for matchforeach $line(@array){if($line =~ /$search1/ && $line =~ /$search2/){$continue++;}}if ($continue > 0){#convert to hashforeach $line(@array){$key=$count++;$value=$line;$names{$key}.="$value";}}else{#go to sub if continue = 0notfound();}#use join function to build value search$delete_value = $search1 . "\|" . $search2;#remove newline characterschomp $delete_value;chomp %names;#get matching key for valuewhile (($key,$value)= each(%names)){if ($value eq $delete_value){#delete key-valuedelete $names{$key};}}#Rewrite the fileopen (INFO,">names_file.txt");while (($key,$value)=each(%names)){print INFO "$value\n";}close (INFO);# Open db for reading and displayopen(INFO, "names_file.txt"); @array=<INFO>;close (INFO);print "Content-type:text/html\n\n"; #Follow with blank lineprint "<html>\n";print "<head><title></title></head>\n";print "<body>\n";print "<table border=\"1\">\n";print "<tr><th>File Contents</th></th></tr>\n";foreach $line (@array){print "<tr><td>$line</td></tr><br>\n";}print "</table>\n";print "</body>\n";print "</html>\n";#This subroutine sends a message to screen if match is not foundsub notfound {print "Content-type:text/html\n\n"; #Follow with blank lineprint "<html>\n";print "<head><title></title></head>\n";print "<body>\n";print "Not found\n";print "</body>\n";print "</html>\n";exit;}
names_file.txt
If you haven't completed the previous exercises copy this data to Notepad and save it in your C:\usr\cgi-bin directory as names_file.txt
Bunyan|PaulSimpson|BartAnderson|PamelaBush|GeorgeNote: 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
How to Test Scripts on Your PC
In order to test scripts of any kind, Perl, PHP or MySQL, on your PC, you'll need to set up a localhost server.
See: Localhost Server
Then read my tutorial on setting up your server to work with the HTMLPad 2010 html editor.
Free Websites
MoonFruit
More websites
Free Website Builders
Yahoo Sitebuilder 2.6
Ucoz
Alleycode HTML
NoteTab Light
Nvu WYSIWYG
Seamonkey Composer
CSE Validator
HTML Kit
Joomla
Web Hosting Services
Web Hosting FAQ
Recommended
Web Hosts
Need Web Hosting?
We Recommend these products
Free Flash Websites


