Are You Wasting Time?

I wasted about 3 years when I first started on the internet.

The first website I built was on free web hosting. Never got more than 2 visitors a day and never made a penny.

Then I tried a web host that I didn't know anything about.
OOPS!
A BIG mistake and another waste of valuable time. My website was down half the time and when I tried to leave they stole my domain name.

I don't want you to make the same mistakes I made. That's why I only recommend top quality, dependable web hosting providers.

I recommend 3 in different price ranges:
Yahoo SB
$11.95 to $39.95

HostPapa
$5.95 to $7.95

CafW Hosting
$4.19 to $10.95

I recommend all 3 for quality and dependability.


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