Posting Form Data Using GET and POST


The <FORM> tag has two important attributes, ACTION and METHOD. The ACTION attribute is assigned to the URL of the form handler, while the METHOD attribute specifies how the data is to be sent to the Web server. If the METHOD attribute is assigned the value GET, the data submitted by the end user is sent in the form of URL parameters. If the METHOD attribute is assigned the value POST, the data submitted by the end user is sent as a separate file.

GET is a one-way method that retrieves files from a server based on a specified URL. POST is a two-way interaction. The <CFHTTP> tag first provides data of various types. The application then returns the data after processing it.

Using GET

You can use the GET method to retrieve a URL. The contents of the retrieved URL are captured in a variable. The variable is displayed as-is or after some operation has been performed on it. The <CFHTTP> tag gets the requested page that's to be stored in a ColdFusion variable or a query object.

The following example shows how to use the <CFHTTP> tag to retrieve a remote Web page and store it as a variable:

 <CFHTTP METHOD="GET"          PROXYSERVER="sample.example.com"          URL=http://www.msn.com          PORT="80"          RESOLVEURL="Yes"          PROXYPORT="80"          THROWONERROR="Yes"> </CFHTTP> <CFOUTPUT>      #CFHTTP.FileContent# </CFOUTPUT> 

CFHTTP.FileContent is a variable where the entire file obtained by <CFHTTP> can be stored. The information contained in this variable can be processed like any other ColdFusion variable. Here are the attributes used in the <CFHTTP> tag:

  • METHOD. This specifies whether the tag uses the HTTP GET or POST method.

  • PROXYSERVER. This is an optional variable used if you need to pass through the proxy server for Internet access.

  • URL. This is a required attribute and specifies the full URL of the remote Web page to be accessed.

  • PORT. This is an optional attribute. The port number is appended to all resolved URLs if this attribute is used in conjunction with the RESOLVEURL attribute.

  • RESOLVEURL. This is set to Yes to restore any partial or relative URLs embedded on the retrieved document. This ensures that all of the links in the document remain valid.

    Note

    In practice, you may find that images from the GET operation aren't displayed correctly. This is because of the difference between relative and absolute URLs. When a Web page is downloaded to the CFHTTP. FileContent variable and displayed using the <CFOUTPUT> tag, the relative paths point to locations on the ColdFusion server. You can use the RESOLVEURL attribute to resolve this problem.

  • THROWONERROR. Specifies whether ColdFusion should generate an exception that can be taken in with the <CFTRY> and <CFCATCH> tags when an error occurs during the execution of the <CFHTTP> tag. It's an optional attribute and has the default value of No. This attribute gets detailed information if the <CFHTTP> tag generates an exception.

You can use the PATH and FILE attributes to save the contents of a downloaded URL on the ColdFusion server. This example shows how to use the <CFHTTP> tag:

 <CFHTTP METHOD="GET"          URL=http://www.park.org          PATH="c:\html"          FILE="park.html"> <CFOUTPUT>#CFHTTP.FileContent#</CFOUTPUT> 

Note

The RESOLVEURL attribute becomes redundant when the PATH and FILE attributes are used. Any content downloaded to a file in this manner contains relative URL references.

The GET method can also retrieve a delimited text file (with commas between the fields) and convert it into a query. Usually, when you use a delimited file, all its contents are saved into the variable CFHTTP.FileContent as-is. The data is in the text form, without the fields.

You can use the attributes NAME, DELIMITER, and TEXTQUALIFIER with GET to make data more useful and save it in appropriate fields. Using these attributes converts the file into a query, which can be used in the normal form in the template.

Just as you can pass the URL parameters to a remote page that you're trying to retrieve, you can also retrieve a page that's generated from a script dynamically based on the parameters passed to it. A search page is an example of such an application.

You can perform searches on several popular sites by submitting HTML forms, which use the HTML GET method. The search criteria, such as a URL parameter, is passed on to a CGI script. The script returns a dynamically generated HTML page that contains the search results. You can use the <CFHTTP> tag and pass the search criteria in the URL.

Using POST

As mentioned earlier, POST sends data to a server for processing. The syntax for the <FORM> tag using the POST method is

 <FORM ACTION="form_handler" METHOD="post> Form contents go here </FORM> 

The datatypes that can be used with POST are listed in the Table 4.1.

Table 4.1: Types of Data Used with POST

Type

Description

URL

The Web server receives the data as if it's appended to the URL using the standard syntax.

FormField

The Web server receives data as if it's entered in a named form field by an end user and submitted using the POST method.

CGI

The Web server receives the data and makes it available as a standard CGI environment variable.

Cookie

The Web server receives the data as if the browser had returned cookie data.

File

The Web server receives the data as an uploaded file.

The <CFHTTPPARAM> tag works with the datatypes listed in Table 4.1. This tag uses the four attributes listed in Table 4.2.

Table 4.2: <CFHTTPPARAM> Attributes

Attribute

Description

NAME

The name of the variable associated with the data being sent to the server

TYPE

The type of data being sent to the server

VALUE

The actual data being sent to the server

FILE

The file to upload to the Web server

Note

<CFHTTPPARAM> tags are enclosed within opening and closing tags. The closing tag is not used with METHOD="GET".

Note

The <CFHTTP> tag is not a multithreaded tag. As a result, you need to use the <CFLOCK> tag to ensure that no two <CFHTTP> tags are executed simultaneously. When a locked <CFHTTP> tag is executed, it obtains a lock. If another <CFHTTP> tag attempts to get a lock, it has to wait till the lock is treed by the first <CFHTTP>.

CFHTTP POST is most widely used to post form-field information to remote applications and scripts. You can pass variables to an application by accepting input from an HTML form using <CFHTTP>. Use <CFOUTPUT> to display the results.

Pass TYPE="URL" in conjunction with form fields such as cookies for applications that require multipart form data in the same operation. TYPE="CGI" in the <CFHTTP> tag provides a method for passing end user-defined CGI environment variables. CGI variables are read-only.

<CFHTTP> can also post files to remote Web servers using the HTTP protocol. This functionality sends a file from a local ColdFusion server to an application located on a remote server for processing. Use TYPE="FILE" only for moving files from the local ColdFusion server to a remote application. The actual processing of the file is done by the remote application.

Note

Use <CFHTTP> to get delimited test file results in the first row of data in the text file being left out for any operation.




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net