3/26/2009

Exercise 9-2

9.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 form.

A.) PERL (study how to use perl for performing form data from
http://www.howtodothings.com/computers/a1297-how-to-access-get--post-request-data-using-perl.html)

A1.) Create a html page named test.html with the following code:














A2.) Create a perl fiel name test_get.pl with the following code:



























Code Explanation:
Line 1: Locate the path of executable perl
Line 2: Inform internet browswer that the below info is a text in html format
Line 4 to 14: It is a function named populatePostFields
Line 5: hash named postFields
Line 6: how many bytes are we supposed to receive?
Line 7: make a list of key/value pairs
Line 8 to 13: cycle through each part and decipher the values
Line 9: get the name/value part strings
Line10: ??? s/%23/\#/g ???? i don't know
Line11: ??? s/%2F/\//g ???? i don't know
Line12: add the part to a list keyed on the name of the variable


A3. Real case Test - please see the below figure
Input some text in the text box and click submit button













It works!!!! The tutorial is from howtodothings that is workable!!

















B.) PYTHON (study PYTHON how to use CGI for performing form data from http://www.python.org/doc/essays/ppt/sd99east/sld041.htm)

B.1.) Create a html named test.hml with following code:














B.2.) Create a python file name test.py with following codes






















Code Explanation:

Line 1: Locate the path of executable perl
Line 3: Import CGI
Line 5-12: Define a function named main used for handling the parsed query info from test.html
Line 7 :Inform internet browswer that the below info is a text in html format
Line 8:Define a variable which is used to take care of decoding and handle POST by cgi.FieldStorage()
Line9-12: if-condition for identify the field named firstname whether is null and has_key only of firstname , if yes, print out "Hello, $firstname, You are handsome", else, print out "Error! Please enter first name"
Line13. execute the function namly main.
B3. Real case Test - please see the below figure

Input "Sam" in the first field and "Kwong" in the second field then click submit button.
















The below result shows the form data that can be successful passed from test.html to test.py

It works!!!! The tutorial is from python.org that is workable!!




























C.1.) Create a html file named testjava.html with the followind codes:


















C.2.) Create a html named jvscript_tst.html which is embedded the javascript in the html page with following code.




















Code Explanation:

Line5-29: Define a function named createRequestObject that is used to request the parsed query string from previous page of URL, simply create an array to store the data from previous url page and use while-loop for query all the data...
Line5: Define a function named createRequestObject();
Line6: Define an object name FORM_DATA which is an array using for store our data
Line7: Define a token that is used to separate data from multi-select inputs
Line8: Define a variable that stores the url info (this.location) and adding a null-string '' forces an implicit type cast from property to string, for NS2 compatibility.
Line10: Use variable named query to store the info after question mark of url
Line11: if-condition that returns false when query.length is smaller than 1
Line12: Defines an object named keypairs to store and keep track of name and value pairs
Line13: Defines a variable name numKP and set its' value to 1 which is a location flag of keypairs[]
Line14-17: Use a while-loop to query all the value between each "&" of URL, and store the result in the array name keypairs.
Line18: Define keypairs[numKP] is equal to query
Line19-24: Use a for-loop to get the URL's varible name and value from the keypairs array, also within the for-loop, there is a while-loop which is used to replace each '+' in data string with a space.
Line25: Set key value to unscape non-alphanumerics
Line26: Generate a separtor-delimited string by appending to what we already have stored
Line30: Set Form_DATA to createRequestObject, which is used to retrieve information with "FORM_DAT[key]=value"
Line34: set "onload" the web page and call the function named createRequestObject
Line36-38: Define varible to store the value which is getting from FORM_DATA[key] and use document.write to print out those value.

C3. Real case Test - please see the below figure
Fill in the my first name and last name in the text box and click button named "Go"







It Works!!!

沒有留言:

發佈留言