(10 points) Create a HTML form address.html to collect people’s names and addres
ID: 665333 • Letter: #
Question
(10 points) Create a HTML form address.html to collect people’s names and addresses. Include 5 textboxes:
1- name
2- address line 1
3- address line 2
4- city
5- zip code
Also include a submit button, a reset button, and a drop down menu with all the states in the US.
(40 points) Create address.cgi to collect and store the address data from address.html into a text file called address.txt . If the user leaves any field blank (except for address line 2), your address.cgi should display an error message and not add any data to the file. Otherwise, it should add a new row. The address.txt file should look like this:
Name, Address1, Address2, City, State, Zip
J Duncan, 901 E 10th street,,Bloomington,Indiana,47408
Jane Smith, 901 E 10th street, Box 27, Bloomington, IN, 47408
John Cooper, 105 S. Woodlawn Ave, Apt 2, Bloomington, IN 4740
…
Remember to use the “a” (append) mode to write to the text file. You should not erase previously entered data. Create the text file with the header information only first. Do not add data to the file when an error occurs.
(10 point BONUS) Modify address.cgi so that it prints out the information currently in address.txt as a nicely formatted table. Include the information just submitted if it was valid.
Explanation / Answer
HTML code for the address.html
<HTML>
<HEAD>
<TITLE>Address Form</TITLE>
</HEAD>
<BODY>
<FORM action="address.cgi" method="GET">
Name: <input type="text" name="name" /><br/>
Address Line 1: <input type="text" name="add_line1" /><br/>
Address Line 2: <input type="text" name="add_line2" /><br/>
City: <input type="text" name="city" /><br/>
Zip: <input type="text" name="zip" /><br/><br/>
<input type="reset" value="Reset" />
<input type="submit" value="Submit" />
</FORM>
</BODY>
</HTML>
CGI code for address.cgi
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read the form's data
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
#add the values to respective variables
$name= $FORM{name};
$add_line1= $FORM{add_line1};
$add_line2= $FORM{add_line2};
$city= $FORM{city};
$zip= $FORM{zip};
#check if any field is empty
if($name==''||$add_line1==''||$add_line2==''||$city==''||$zip==''){
print "Content-type:text/html ";
print "One of the required field is empty";
}else{
#open the address.txt file to write the data
open(OUT, ">>address.txt") or &dienice("Couldn't open output file: $!");
print OUT "$name, $add_line1, $add_line2, $city, $zip";
print OUT " ";
close(OUT);
}
#open the address.txt file to read existing data
open(INF,"address.txt") or dienice("Can't open address.txt: $!");
#read the entire file into ary
@ary = <INF>;
close(INF);
#display each line of ary
foreach $line (@ary) {
chomp($line);
print "$line ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.