www.createasite.net
freewh.890m.com

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.pl
print "Content-type:text/html\n\n"; #Content Header

print "<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";

Test this Script


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 data
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";
}
$search1=$formdata{'search1'};
$search2=$formdata{'search2'};

# Open db for reading and display
open(INFO, "names_file.txt");
@array=<INFO>;
close (INFO);

$count=0;
$continue=0;

#check for match
foreach $line(@array){
if($line =~ /$search1/ && $line =~ /$search2/){
$continue++;
}
}

if ($continue > 0){
#convert to hash
foreach $line(@array){
$key=$count++;
$value=$line;
$names{$key}.="$value";
}

}else{
#go to sub if continue = 0
notfound();
}

#use join function to build value search
$delete_value = $search1 . "\|" . $search2;
#remove newline characters
chomp $delete_value;
chomp %names;
#get matching key for value
while (($key,$value)= each(%names)){
if ($value eq $delete_value){
#delete key-value
delete $names{$key};
}
}

#Rewrite the file
open (INFO,">names_file.txt");
while (($key,$value)=each(%names)){
print INFO "$value\n";
}
close (INFO);

# Open db for reading and display
open(INFO, "names_file.txt");
@array=<INFO>;
close (INFO);


print "Content-type:text/html\n\n"; #Follow with blank line

print "<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 found
sub notfound {
print "Content-type:text/html\n\n"; #Follow with blank line

print "<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|Paul
Simpson|Bart
Anderson|Pamela
Bush|George

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 #4

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 #6

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

|HTML OnLine | Website Tutorial | Home |