[field1_name]=[field1_data]&[field2_name]=[field2_data]&...&[fieldn_name]=[fieldn_data]
In order to make sense of this mess, you will need to break it back up into its various pieces of information about each field. To do this I recommend using the Tokenize OSAX. The Tokenize OSAX is designed to take in data that has key characters as delimiters and return a list of items with each item representing the data between two delimiters (read the documentation that came with Tokenize for a better explanation).
This lesson is the first one where you use an OSAX (AppleScript Language Extension). If you are not familiar with how they work, get a book on AppleScript and read up about it. As a basic explanation, OSAX act as extensions to the AppleScript language. They either allow you to do things you couldn't do in AppleScript (such as listing all aliases on a disk) or they do something very fast that would take many lines in AppleScript. I may provide some quick information later, but it probably won't be nearly as nice as the BMUG guide or Danny Goodman's book.
NOTE: if you have not yet installed this OSAX, then do it before starting this lesson. The script will not compile without it. Go back to the Requirements section to download the OSAX if you need it.
property crlf : (ASCII character 13) & (ASCII character 10) property http_10_header : "HTTP/1.0 200 OK" & crlf & "Server: MacHTTP" & crlf & Ā "MIME-Version: 1.0" & crlf & "Content-type: text/html" & crlf & crlf property idletime : 300 property datestamp : 0 set datestamp to current date on «event WWW½sdoc» path_args ¬ given «class kfor»:http_search_args, ¬ «class post»:post_args, «class meth»:method, ¬ «class addr»:client_address, «class user»:username, ¬ «class pass»:password, «class frmu»:from_user, ¬ «class svnm»:server_name, «class svpt»:server_port, ¬ «class scnm»:script_name, «class ctyp»:content_type try set datestamp to current date set return_page to http_10_header ¬ & "<HTML><HEAD><TITLE>Parsed Results</TITLE></HEAD>" ¬ & "<BODY><H1>Parsed Results</H1>" & return set return_page to return_page & "<H4>post_args</H4><PRE>" & return set postarglist to tokenize post_args with delimiters {"&"} set postargtext to "" repeat with curritem in postarglist set postargtext to postargtext & curritem & return end repeat set return_page to return_page ¬ & postargtext & "</PRE>" & return set return_page to return_page ¬ & "<HR><I>Results generated at: " & (current date) ¬ & "</I>" & "</BODY></HTML>" return return_page on error errMsg number errNum set return_page to http_10_header ¬ & "<HTML><HEAD><TITLE>Error Page</TITLE></HEAD>" ¬ & "<BODY><H1>Error Encountered!</H1>" & return ¬ & "An error was encountered while trying to run this script." & return set return_page to return_page ¬ & "<H3>Error Message</H3>" & return & errMsg & return ¬ & "<H3>Error Number</H3>" & return & errNum & return ¬ & "<H3>Date</H3>" & return & (current date) & return set return_page to return_page ¬ & "<HR>Please notify Jon Wiederspan at " ¬ & "<A HREF=\"mailto:jonwd@tjp.washington.edu\">jonwd@tjp.washington.edu</A>" ¬ & " of this error." & "</BODY></HTML>" return return_page end try end «event WWW½sdoc» on idle if (current date) > (datestamp + idletime) then quit end if return 5 end idle on quit continue quit end quit
The first line to look at is this one:
set postarglist to tokenize post_args with delimiters {"&"}This line takes in the data in post_args and separates it into a list using the "&" as the item delimiter (Note: As a side effect, all of the "&" characters in the text are removed). The output (postarglist) is a list which looks something like this:
{field1_name=field1_data,field2_name=field2_data,...,fieldn_name=fieldn_data}(Note: This is not text. This is a list, which is a special type in AppleScript. If you don't know what lists are, refer back to your AppleScript guide).
This is a very workable format now for getting at the real information that the user typed into the form. The next step is to run through the list one item at a time and make a nice, readable format of the data. The following code takes care of that:
set postargtext to "" repeat with curritem in postarglist set postargtext to postargtext & curritem & return end repeatThis code uses the "repeat with" control structure to repeat an action for each item in a list. All that is done is to append each item to postargtext as text with a return at the end. You will see the difference when you test the script below.
I've chosen to use an OSAX here to help with parsing the data. There are two reasons why this is a good thing to do.
Of course, noone is forcing you to use these OSAXen. Perhaps you're one of those people who still try to write major applications for the Apple II just to show that it can be done. If so, you may be interested in the code segment below. This is what you would have had to type in AppleScript to do the same parsing without using Tokenize:
set oldDelim to AppleScript's text item delimiters set AppleScript's text item delimiters to {"&"} set postarglist to text items of post_args set AppleScript's text item delimiters to oldDelimI know what you're thinking. You're saying "That wasn't so bad. What a wimp he is, running off to find an OSAX to replace four measly lines". Well, I haven't timed the script to see how much faster it is, but I can guarantee that it will be noticeable if you start parsing a large form with 30 or 50 different items in it. (If you do try this and it isn't faster, I don't want to know.)
There is another similar problem. As I may have said several times now, there are some clients, notably NCSA Mosaic and Netscape, that encode spaces as "+" instead of as "%20". This is a pain in the neck not only because it creates a special case while decoding, but also because it means there is another special character (the + sign) that needs to be encoded by the client. We will address this in later lessons, but if it bugs you as much as it does me, send a note to the developers and ask them to please stop doing this.