
822
DEVELOPING
COLDFUSION 9 APPLICATIONS
Requesting and Presenting Information
Las
t
upda
te
d 8/5/2010
The following example shows how to use the
SetForm
tag to submit the contents of a login form. When the user clicks
the Login! button, the doLogin function calls the proxy
setForm
function and then the
AuthenticationSystem.cfc
validateCredentials
method. The
validateCredentials
method checks the password of the user and if it is
valid, returns
true
to the proxy. Because the proxy is synchronous (the default), the
doLogin
method gets the returned
value. If the value is
true
, it hides the login window; the user can then access the page contents. If the return value is
false
, the
doLogin
function displays a message in the login window title bar.
The following example shows the setForm.cfm application:
<html>
<head>
<script type="text/javascript">
function doLogin() {
// Create the Ajax proxy instance.
var auth = new AuthenticationSystem();
// setForm() implicitly passes the form fields to the CFC function.
auth.setForm("loginForm");
//Call the CFC validateCredentials function.
if (auth.validateCredentials()) {
ColdFusion.Window.hide("loginWindow");
} else {
var msg = document.getElementById("loginWindow_title");
msg.innerHTML = "Incorrect username/password. Please try again!";
}
}
</script>
</head>
<body>
<cfajaxproxy cfc="AuthenticationSystem" />
<cfif structKeyExists(URL,"logout") and URL.logout>
<cflogout />
</cfif>
<cflogin>
<cfwindow name="loginWindow" center="true" closable="false"
draggable="false" modal="true"
title="Please login to use this system"
initshow="true" width="400" height="200">
<!--- Notice that the form does not have a submit button.