How can I work with remote HTML forms?
The nature of working with HTML form submission (or handler) pages is such that by default they do not make any distinction in regards to the source of the form data. In other words, if a form handler page is expecting to receive the posting of a name and an address value, it does not know (or care) if the form date came from a page on your server, or from another server. Take for example many of the web based credit card merchant account systems. The typical scenario for these systems is for you to provide a form on your website that contains the form data you have collected for a shopping transaction. You collect information such as the customer's name, billing address, and credit card number in a form. You then post this form data to a specifically designed page on the credit card processing company's website. This processing page performs all the magic of the credit card transaction, and then informs the user of the outcome. The big problem with this scenario is that your customer has now left your website, and this is not the ideal solution or user experience.
Amongst other functionality, the ColdFusion <cfhttp> tag provides a way for you to post form data to a remote form, and then retrieve the results that are outputted from the form results page into a ColdFusion variable. You can then process the results variable (the form submission outcome) within your ColdFusion application. So with our above credit card processing example, we could let <cfhttp> leave our website, post the form data, retrieve the results of the credit card transaction, then seamlessly display the outcome to the user, all from within our website.
In order to use <cfhttp> to post to a remote form, you need to know the full address of the template you will be posting to. You also need to know each expected form parameter, and include a <cfhttpparam> tag for each of these parameters. A sample <cfhttp> form post would look as follows:
<cfhttpparam type="formField" name="creditCardNumber" value="#localCCNum#">
<cfhttpparam type="formField" name="userName" value="#localName#">
</cfhttp>
Notice how we can pass in any value (in this case we are using the dynamic contents of ColdFusion variables) to the remote form via the <cfhttpparam> value parameter.
The results of the form post will be contained in the cfhttp.fileContent variable. You can access this variable to parse out and redisplay any data. In our credit card processing scenario, we would probably want to parse out the transaction status message so we could dynamically show the user a success or failure message.
<cfhttp> is a very powerful tool, and it can be used for many things. Some other common <cfhttp> tasks include grabbing current weather and stock quotes. One final thing to remember about <cfhttp>: you are not limited to ColdFusion pages. A form is a form, so feel free to use <cfhttp> to communicate with any other flavor of form, including those made with JSP, ASP, PHP, etc.
This question was written by Jeremy Petersen.
It was last updated on February 1, 2006 at 9:23:43 AM EST.
CFML Referenced
Categories
Comments
Comment made by Paul Carney on February 2, 2006 at 11:30 AM
An added item I do many times: if you need that cfhttp session to persist over multiple calls (like if you are logging in the first time), you can interrogate the "responseHeader" structure returned from the call and look for "set-cookie" values.
After you have looped through and built a array of these cookie values, you can simply loop through that array when you make your next cfhttp call in order to send cookie-type parameters to use the previous session.
Comment made by gopal nandakumar on August 19, 2006 at 10:33 AM
Why does CFHTTP posts the data more than once for a single CFHTTP command? This happans CFHTTP accessed page has a CFHTTP code in it.
Comment made by Raymond Camden on August 19, 2006 at 8:26 PM
I haven't seen that gopal. If you have, you should report it as a bug.