“Retrofit” the form data string above for buying some French perfume into the HTML form fields and submit button on the Web page form.
Form of Retrofit:

The coding:

When click on 'Submit' button, the data string: "name=Dennis&card=visa&number=1234567890123456&order=French+perfume&submit=+Submit+" submitted to the server. As the missing of server side program, an error show bellow.

2. Write the script
Script archives exist for PERL, Python and JavaScript. Search the Web for a script that processes the HTML forms data. Read the code and list the steps involved in processing the form.
I found a CGI script from CGI101.COM Web site at http://www.cgi101.com/book/ch3/text.html, the script named get.cgi runs in the server to process the form named getform.html that submitted by the client:
----------------------------------------------------------------------------
#!/usr/bin/perl -wT----------------------------------------------------------------------------
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
print header;
print start_html("Get Form");
my %form;
foreach my $p (param()) {
$form{$p} = param($p);
print "$p = $form{$p}<br>\n";
}
print end_html;
The first line #!, indicates that this is a script. The next part /usr/bin/perl, is the location of the Perl interpeter. The final part contains optional flags for the Perl interpreter. Warnings are enabled by the -w flags. Special user input taint checking is enabled by the -T flag.
The second line use, includes the module from standard library that provide a number of useful functions and features for writing CGI programs. The following functions in the above script is come from that module:
The print start_html("Thank You") defines the page title as "Thank You".
The end_html, prints the footer.
The third line, use CGI:: Carp..., causes all warnings and fatal error messages to be echoned in the browser window for program debugging.
The fourth line, use strict, is a standard Perl module that requires the programmers to declare all variables for good programming style.
After print out the 'Thank you' on the return screen, the foreach loop read the variables in the form of the call program and print them out to the screen line by line.
The screen of getform.html:

The screen of get.cgi:

3. Can you modify the script to process the form?
Yes, I can modify the CGI script of the post.cgi to process the form "Retrofit".
As the CGI server setting in my computer is not the same as the above example. I use the command to find out the Perl location in my computer and modify the first line in the script to:
#! /opt/local/bin/perl -wT

The second line use, includes the module from standard library that provide a number of useful functions and features for writing CGI programs. The following functions in the above script is come from that module:
- header;
- start_html;
- end_html;
The print start_html("Thank You") defines the page title as "Thank You".
The end_html, prints the footer.
The third line, use CGI:: Carp..., causes all warnings and fatal error messages to be echoned in the browser window for program debugging.
The fourth line, use strict, is a standard Perl module that requires the programmers to declare all variables for good programming style.
After print out the 'Thank you' on the return screen, the foreach loop read the variables in the form of the call program and print them out to the screen line by line.
The screen of getform.html:

The screen of get.cgi:

3. Can you modify the script to process the form?
Yes, I can modify the CGI script of the post.cgi to process the form "Retrofit".
As the CGI server setting in my computer is not the same as the above example. I use the command to find out the Perl location in my computer and modify the first line in the script to:
#! /opt/local/bin/perl -wT

No comments:
Post a Comment