Tuesday, April 13, 2010

Topic 4 - Exercise 6


Exercise 6

1. Design the form 'Retrofit' to form data string:

name=Evan+Burke&Card=Visa&Number=8443261344895544&order=French+perfume

for buying some French perfume into the HTML form field and submitt button on the Web page form.

Answer:

The code in the image above was used to crate a simple form in Dreamweaver. Click on the image to enlarge.

2. Write a script that processes the HTML form data. Read the code and list the steps involved in processing the form. 

Answer: 

1 open (IN, "file1.htm");
2 open (OUT, ">new_file1.htm");
3 while ($line = [IN]) {
4 $line =~ s/[h1]/[h1 class="big"]/;
5 (print OUT $line);
6 }
7 close (IN);
8 close (OUT);


Line 1
In this line file1.htm is opened so that it can be processed by the script. In order to process the file, Perl uses something called a filehandle, which provides a kind of link between the script and the operating system, containing information about the file that is being processed. I’ve called this "opening" filehandle ‘IN’, but I could have used anything within reason. Filehandles are normally in capitals.

Line 2
This line creates a new file called ‘new_file1.htm’, which is written to by using another filehandle, OUT. The ‘>’ just before the filename indicates that the file will be written to.

Line 3
This line sets up a loop in which each line in file1.htm will be examined individually.

Line 4
This is the regular expression. It searches for one occurrence of [h1] on each line of file1.htm and, if it finds one, changes it to [h1 class="big"].

Looking at Line 4 in more detail:
$line - This is a variable that contains a line of text. It gets modified if the substitution is successful.
=~ is called the comparison operator.
s is the substitution operator.
[h1] is what needs to be substituted (replaced).
[h1 class="big"] is what [h1] has to be changed to.

Line 5
This line takes the contents of the $line variable and, via the OUT file handle, writes the line to new_file1.htm.

Line 6
This line closes the ‘while’ loop. The loop is repeated until all the lines in file1.htm have been examined.

Lines 7 and 8
These two lines close the two file handles that have been used in the script. If you missed off these two lines the script would still work, but it’s good programming practice to close file handles, thus freeing up the file handle name so that it can be used, for example, by another file.

3. Improve the user experience by adding the Javascript feature:

Answer:

This bit of code found under the following link:

http://www.javascriptfreecode.com/13.htm

adds Google search box to the website and the result is ass follows:















 


No comments:

Post a Comment