HTTP Forms
One of the most common ways to communicate with a server is over HTTP. Curl's
HttForm class acts as a container for controls that will be part of the web form. Users complete the form by modifying its controls before submitting hte form to an agent for processing. The function of
HttpForm is similar to the
FORM tag in HTML.
Lets look at the following simple example. In this case, we are creating a Google query that also specifies the language and the interface. Enter text that you would like to query in Google, e.g. "Baseball". Then hit thie "Submit" button. A new browser window will be launched to display the results.
In this case,
HttpForm is used to create the Web form
- A Dialog is created to contain the controls
- The user fills out the form and clicks the Submit button
- Key/value pairs are sent to the server script using GET or POST
- Returned information is sent to the user
When instantiating an
HttpForm object, you provide the URL of the CGI program or script on the Web site that will receive the request, as well as the HTTP request method of
GET or
POST.
Let's break down this example. First we will create an form that only submits the query. Afterwards, we can add the additional query elements and expand the example.
In this example, we will be sending the following HTTP request using
GET:
http://www.google.com/search?q=Baseball (where "Baseball" represents the text typed in by the user). In order to accomplish this, we need to first specify the
url of the server.
{url "http://www.google.com/search"},
The
target specifies the browser window in which the action will be displayed. Values are as follows:
- _self: Returns results in the current browser window (default)
- _blank: Returns results in a new browser window
- _parent: Returns results in the parent frameset of the current frame.
- _top: Returns the results in the top-level window (full browser window)
Since the
HttpForm is a
Dialog, we must create one child object that can contain multiple children. In our example, we are using a
VBox. Control values that are passed to the HTTP request must include the
name attribute. The
TextField has
name = "q" defined.
{TextField
width = 2in,
name = "q"
}
The
submit-button is a Curl procedure that creates a
CommandBuitton with at "Submit" label. When the user clicks on the button, it automatically passes the form information to the specified
Url.
{submit-button}
We have created this proc as a convenience procedure. For custom submit functionality, you can always create your own procedure or method. Another convenience procedure that we have created for you is
reset-button. In this case, it creates and returns a standard "Reset" button for use in the
HttpForm. Let's add this functionality to our example.
Note that the reset button will 'reset' all controls to their default state. In the above example the initial value has been set to "Pizza". If no initial value is specified, the "Reset" button will leave the
TextField blank.