Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

well formed form help

Status
Not open for further replies.

doyle9732

Programmer
Apr 12, 2002
185
0
0
CA
In the past, I have always had my error checking code used the <cfabort> and had the user use the browser back button to go back and correct their input. But, on a previous post someone mentioned that that was poor practice and that I should be reloading the form with error messages showing up on the actual form. So, I am at the start of a new project and want to follow this practice, but having never used it, would like to see an example of it. Does anyone have a piece of code (just a few input fields) that shows this process?

Many thanks!
stephanie
 
Stephanie, first you need a list of error checks to make against the form submission and the message to return to the user. Like, 'all fields required', 'email is not valid', 'password must be 5 characters long', etc.

You perform all the error check, insert into/update the dB on an action page, not the same page as the form. The reason is if the user keeps hitting the refresh button on the browser over and over, any code on that page will keep re-running, including insert into/update SQL code. Having an action page is much cleaner and easier to debug.

You wrap all error checking in a <cfif>/</cfif> statement, like:
Code:
<cfif FORM.Name NEQ "" and FORM.Email NEQ "">
  <!--- if all is good, perform any task you want --->
<cfelse>
  <!-- status 1: required fields missing
  <cflocation url="formPage.cfm?status=1 addtoken="no">
</cfif>

Then back on the form page, you have a table cell that would display the error message template page, like:
Code:
<cfif isdefined("URL.status")>
  <cfinclude template="statusMessages.cfm">
</cfif>

And, finally, create a statusMessage.cfm template that will have all your error message code that will load appropriately on any page that calls this template. This way you can use the same template site wide, and any change will be made on one page template page and not on all individual pages.

The statusMessages.cfm can look like:
Code:
<cfif isdefined("URL.Status")>
  <cfif status EQ 1>Required fields missing.</cfif>
  <cfif status EQ 2>Not a valid email.</cfif>
  <cfif status EQ 3>Thank you for registering.</cfif>
</cfif>

There are many ways to do this, and i'm sure someone else will reply with another method. In the end you stick with a method that fits your needs.

Does this help?

[sub]
____________________________________
Just Imagine.
[sub]
 
BTW, almost forgot, make sure to wrap you're any database insertion/update code within <cftry><cfcatch> this way if an error does occur you have a error trap already set up. What I usuall do is:

Code:
<cftry>
  <cfquery name="" datasource="">
    insert into
    values
  </cfquery>
<cfcatch type="any">
  <cfmail to="" from="" subject="">
    Please be advised there was error on #cgi.script_name# at #dateformat(now(),'MM/DD/YYYY')# at #timeformat(now(),'HH:MM:SS tt')#.
    <cfdump var="#cfcatch#" label="query error">
  </cfmail>
</cfcatch>
</cftry>

[sub]
____________________________________
Just Imagine.
[sub]
 
thanks! I'll give it a try this way.
Stephanie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top