ColdFusion 8 - Async Form Submission

Posted By : todd sharp Posted At : June 20, 2007 1:39 PM Posted In: ColdFusion

16

ColdFusion 8 gives us a very cool and easy to use API for async form submission. In this demo we have a simple form:

<cfpod name="formPod" title="Enter your info..." width="300" height="400">
    <cfform name="testForm">
        Name: <cfinput type="text" name="name" value="#form.name#"><br />
        Phone Number: <cfinput type="text" name="phone" value="#form.phone#"><br />
        Email: <cfinput type="text" name="email" value="#form.email#"><br />
        Fake An Error? <cfinput type="checkbox" name="fakeError" checked="false" value="true" /><br />
        <cfinput type="button" name="submit" value="Spam Me" onclick="javascript:submitFrm();">
    </cfform>
</cfpod>

Notice my 'submit' button (just a button really) calls the JavaScript function submitFrm(). This function is quite trivial:

submitFrm = function(){
ColdFusion.Ajax.submitForm('testForm', 'formAction.cfm', myCB, myError);
}

Here is use ColdFusion's built in JavaScript API function ColdFusion.Ajax.submitForm. This function uses the following syntax:

ColdFusion.Ajax.submitForm(formId, URL[, callbackHandler[, errorHandler[,
httpMethod[, asynch]]]])

The form id and URL are obvious. The next two optional attributes are a bit new. They specify a callback and error handling JavaScript function to fire. The callback handler takes one argument - the response from a successful form submission. The error handler takes two arguments - the HTTP status code and error message of any errors thrown in the form submission. The final two optional arguments are the httpMethod (defaults to POST) and a Boolean asynch (defaults to true).

So in my demo I have a simple callback and error handler:

myCB = function(resp){
var msgDiv = document.getElementById('messages');
msgDiv.innerHTML = resp;
}

myError = function(sc,msg){
alert('Error!!! ' +sc +' '+msg);
}

My callback takes the response and shoves it in a simple div. This could be a confirmation message or validation error messages, etc. The error handler in this example just displays a simple alert message.

My action page is pretty trivial as well - just a handfull of validation checks on the form data and it outputs simple HTML with either errors or a confirmation message.

Check out the demo. I output the current time at the top of the page to illustrate the main page content being static.

Comments (16)

John H.'s Gravatar I dislike forms where I have to click to submit rather the natural way of just hitting enter.

To preserve submit on enter as well as the button click, change the 'button' to type 'submit'. Then move your submitFrm function call to the form onSubmit handler. You need to add a return false at the end of submitFrm() to prevent regular form submission.

todd sharp's Gravatar Point well taken...

Dale Fraser's Gravatar I notice these scripts live in CFIDE how do you use this without compromising security and openening access to CF Administrator?

dc's Gravatar @Dale

Use your webserver security to only allow public access to the /CFIDE/scripts, all other access to /CFIDE URLs are disabled and security is not compromised.

Dale Fraser's Gravatar Yeah,

Good plan, someone should tell the hosting provider.

http://h127171.cf8beta.com/CFIDE/administrator/ind...

Dale Fraser's Gravatar Also,

I know a lot about IIS, but im not sure how I would even lock it down the way you are describing.

todd sharp's Gravatar Dale: This is free hosting after all - so I won't be putting anything up there that I'd be worried about.

I think the issue in my case was that they physically had to put a copy of /CFIDE in my root because the virtual dirs weren't working.

todd sharp's Gravatar PS Dale - I'm not a server admin and don't know much about securing CFIDE - but I do know that CF8 is not the first release to use scripts in /CFIDE - many of the built in validation scripts for cfform have been in there.

dc's Gravatar @Dale

Don't use your host then. It is your choice.

Apache: Use Location Directive along with the Order directive to allow/deny URLs

IIS: A quick check of the docs (haven't used since version 4) brings up the following: http://www.microsoft.com/technet/prodtechnol/Windo...

todd sharp's Gravatar DC - he was referring to my host - and again - this is free CF8 beta hosting from HostMySite that I'm using for these CF8 demos.

dc's Gravatar cool, misread that bit, sorry.

Bard's Gravatar Hi,

is it possible to provide a target (pod, layoutarea etc.) which is refreshed, when the form is submitted - to which the form is submitted?

All the best to you,
Bard

todd sharp's Gravatar If I understand your question you could just add a ColdFusion.naviate() function inside the call back function to refresh a container upon success.

Bard's Gravatar Thanks for your reply. I call Javascript ColdFusion.getFormQueryString('myform') and append it's result to the script name in the navigate-method, which in fact is sending a form with the GET-method to another cflayoutarea.
In the callback I call navigate to refresh a third cflayoutarea to show a list of files generated by the first script. Works beautifully.

Gavy's Gravatar Hi Thanks Good Stuff, I checked and my problems where i use <div id="messages"></div>

it loads the result first time good and there after it does not display the result. i check in firebug and there it appears with lot of whitespace.

Can u please answer me what is wrong with this

Jim's Gravatar See -> http://www.coldfusionjedi.com/index.cfm/2008/7/3/A... for good commentary on the whitespace...