image ResEx Logo
ResExcellence www : Powered by Google
Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Articles
   3D
   Audio
   Custom Controls
   General RB
   Graphics
   Hacks
   Mac OS X
   Menus
   Novelty
   Printing
   REALbasic 2005
   REALbasic 2006
   Registration
   Resources
   Reviews
   Serial
   Speech
   Sockets
   XML
   Video
Resource Links
News
   Current News
   February 2006
   January 2006
   December 2005
   November 2005
   October 2005
   September 2005
   August 2005
   July 2005
   June 2005
   May 2005
   April 2005
   March 2005









REALbasic for Dummies
by Erick Tejkowski


Learning REALbasic through Applications
by Clayton E., Crooks II


REALbasic for Macintosh
by Michael Swaine


REALbasic Cross-Platform Application Development
by Mark S. Choate





Older files are in Stuffit 5 or greater format. Newer files are ".Zip". Download StuffIt Expander
Tell us about a bad link. Thank You!

Web Form Submission by Seth Willits
09-25-04

Printer Version




GET/POST Forms
There are two types of requests that pretty much drive the internet. There's a GET and a POST. If you ever been to a website and submitted a search form and saw all sorts of stuff after a question mark in the URL (ie, Google's website does this), that's a GET form. Unlike a GET form where the parameters for the request are part of the URL string, POST forms submit additional parameters (other than the URL itself) in a way that is hidden from the user. POST forms are usually used for forms that contain sensitive user information, or information the site doesn't want you to see (like field names or something). REALbasic offers a way to submit form data for either GET or POST forms (the only difference is the method name you call), and it's A LOT easier to do it now with an HTTPSocket than it was using a regular TCP Socket years ago.


The Form To Submit
In this tutorial, we're going to submit a form which will search one of the archives of the REALbasic mailing lists. The forms for each list (the NUG, Betas, Games, Tips, and Getting Started) are all identical except for one form field which specifies which list you're searching. The other four fields are the seach string, how many results to display, the length of each results description, and how to sort the results.

When submitting form data, it's simply a list of key/value pairs sent to a certain page with a form processing script, where the keys are the names of the html fields and the values are what the user types in. So how do you find the names of the fields and the page to send the request to? Look at the html of the form.

The form url and type (GET or POST) will be specified in the opening <form> tag:
<form method="get" action="/listarchives/cgi-bin/namazu.cgi">

The URL we'll send the request to is in the action attribute. Now remember, this is a URL relative to the site you're working on, so in this case it would be "http://www.realsoftware.com/listarchives/cgi-bin/namazu.cgi". The type of form is in the method attribute (GET).

The field names are the in the "name" attributes of the various fields in the form. While the field values are either explicitly or implicitly the "value" attribute. By that, I mean if the field is a text field, a "value" isn't initially supplied, so you won't find a value attribute in the html. In the case of the search field we have: <input type="text" name="query" size="40">

This shows that the field name for the search field is "query", and the value will be determined by the user, unlike the description popup, where the values for the choices are given:

<select name="result">
   <option selected value="normal">normal</option>
   <option value="short">short</option>
</select>

In this case, if the name of the field is "result" (and odd name, but that's what it is) and the value will either be "normal" or "short" depending on which option is selected.



The Design
So, I'll skip going through the rest of the html and we'll move straight into the example. Below is an image of the layout of the window. There's a search field named EFQuery, a button named BtnSearch, four popups named PopDisplay, PopDescription, PopSort, and PopList, a progress bar named ProgressBar1, a giant result field (which will contain the html of the result page) named EFResult, and an HTTPSocket instance named HTTPSocket1.



With the Display and Description popups, the form field values are exactly the same as the options listed in their popups. For example "normal" is the field value for the "normal" option in the Description popup. This makes it pretty easy when we're building the GET URL because we can take the "Text" value right out of the popup. This is different from the Sort popup though which has a different value than the text in the popup. So what we do first is specify RowTag values for each row of the popup that match perfectly with the initial value of the popup (see the finished project). We'll use these later.




The BtnSearch Action Event
Next, the general idea is to have the user fill everything out in the interface and click the search button. A dictionary instance is then created with the field name/value pairs as you can see below. The first value specified (submit/Search!) is the search button. Yes, buttons are considered fields too. Often, there are times where two or three buttons are in a form and you need to know which one was clicked. In this case there is only one button, so theoretically the page wouldn't need to know which button was clicked, but you never know, so stick it in anyway.

After building the dictionary, you then fire off the request to the HTTPSocket, specifying the Action parameter of the form as the URL, followed by a question mark (?) which basically says "there is form data after the url", and then the form data itself. Rather than having to write the form data in a url encoded format (?submit=Search&query=blah+blah+blah&idxname=.....) you simply use the EncodeFormData method which returns the dictionary in that format for you. Very handy!




The HTTPSocket
After that we have to handle whatever happens in the HTTPSocket. If an error occurs with the socket (could not connect to the server, violently disconnected, or something), we handle it in the Error event.



If an error happens on the server, we have to handle that in the PageReceived event (or the DownloadComplete event if we had specified a folderitem to download to in the Get method). The server will send back a status message along with whatever result comes back. The "OK" status message (everything went as planned) is "200". There is a list of all status messages on the w3 website.



The content parameter contains the html of the resulting page, and we simply display that in the EFResults field.



Finished
Well, there ya go. Submitting form data is actually very easy. If you needed to submit a POST form, all you'd need to do is change from using the Get method, to using the Post method of the socket. I think the hardest part is actually finding the field names in the html page. The rest of it is very simple, and is so because the guys at REAL Software did a great job of making it as easy as possible. As always, you can download the project here.






Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement. [an error occurred while processing this directive]