<CFAJAXIMPORT />
<cfform name="frm" method="post" id="frm">
Name:
<cfinput id="Name" name="Name" />
<div id="NameError"></div>
</div>
Email Address :
<cfinput id="emailAddress" name="emailAddress" />
<div id="emailAddressError"></div>
</div>
<div><cfinput name="submit" type="button" value="submit" onclick="Formsubmition();"></div>
</cfform>
<script>
function Formsubmition() {
document.getElementById("NameError").innerHTML = '';
document.getElementById("emailAddressError").innerHTML = '';
ColdFusion.Ajax.submitForm('frm','action.cfm', responsecallback, errorHandler);
}
function responsecallback (response){
var errors = ColdFusion.JSON.decode(response);
var errorexist = false;
for(i in errors){
document.getElementById(i+"Error").innerHTML = errors[i];
errorexist = true;
}
if(! errorexist){
alert('Data Inserted');
}
}
function errorHandler(id, message) {
alert("Error while inserting\n Error code: "+id+"\n Message: "+message);
}
</script>
<cfsetting enablecfoutputonly="true" />
<cfset errors = StructNew() />
<cfif form.Name EQ "">
<cfset errors["Name"] = "You must enter a first name." />
</cfif>
<cfif NOT isValid("email", form.emailAddress)>
<cfset errors["emailAddress"]= "You must enter a valid email address" />
</cfif>
<cfif structIsEmpty(errors)>
<!--- insert in to db --->
<cfelse>
<cfoutput><cfoutput>#serializeJSON(errors)#</cfoutput></cfoutput>
</cfif>
<cfsetting enablecfoutputonly="false" />
function responsecallback(response){
var errors = ColdFusion.JSON.decode(response);
var errorexist = false;
for(i in errors){
document.getElementById(i+"Error").innerHTML = errors[i];
errorexist = true;
}
if(! errorexist){
alert('Data Inserted');
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)