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!

server side form validation

Status
Not open for further replies.

myatia

Programmer
Nov 21, 2002
232
0
0
Hi,

I'm trying to validate the date in a form on the page "form.cfm". It is submitted to "form_action.cfm", where I validate the form data. In the event that there are errors, I want to pass back all of the form values to "form.cfm" so that they are pre-displayed in the form, along with a message describing the errors. My question is, what is the best way to get those values back to "form.cfm"? I don't want to pass them through the URL. Hrm...I guess I could just include form_action.cfm as a template at the top of form.cfm, but is there any other way to pass values between pages without using URL or SESSION variables? If anyone has any suggestions, I'd really appreciate them.

Thanks,

Misty
 
Hi mate,

Use hidden form fields and the POST method.

Other method which saves code is to submit the form itself, check for a hidden field from the form and if it exists, validate.

Based on the validation either show the errors and populate the data back into the form or process the data.

Hope this helps Wullie


The pessimist complains about the wind. The optimist expects it to change.
The leader adjusts the sails. - John Maxwell
 
If I use hidden form fields and the Post method, how do I get the form to actually submit without using something like onLoad="document.form.submit()" in JavaScript?

Thanks,

Misty
 
Okay, if I have a form like this

<FORM ACTION=&quot;form.cfm&quot; METHOD=&quot;Post&quot;>
<INPUT TYPE=&quot;Hidden&quot; NAME=&quot;MyField&quot; VALUE=&quot;1&quot;>
</FORM>

it's not going to just automatically submit, right? Don't I need some event, like the pressing of a submit button, to trigger the form's submission? I don't want the user to have to press two submit buttons (one of form.cfm, one on form_action.cfm) to submit their data once. How do I get the form on form_action.cfm to submit itself without using JavaScript? Am I missing something?

Thanks,

Misty
 
to answer the immediate question, you would need a submit button... otherwise the form won't ever do anything (unless you use javascript, as you mentioned).

Code:
<FORM ACTION=&quot;form.cfm&quot; METHOD=&quot;Post&quot;>
   <INPUT TYPE=&quot;Hidden&quot; NAME=&quot;MyField&quot; VALUE=&quot;1&quot;>
   <INPUT TYPE=&quot;Submit&quot; NAME=&quot;Submit&quot; VALUE=&quot;Go back and try again&quot;>
</FORM>

But to answer your initial question, the way I've always handled something like this is, as you were starting to say, have the form_action page included in the form page:

form.cfm-
Code:
<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;    
&quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]
<CFPARAM name=&quot;FORM.myField&quot; default=&quot;&quot;>
<CFSET errorMessage = &quot;&quot;>

<CFIF IsDefined(&quot;FORM.submit&quot;)>
	<CFIF Len(Trim(FORM.myField)) GT 0>
		<CFIF (ListLen(&quot;#FORM.myField#&quot;,&quot;@&quot;) EQ 2) AND (Find(&quot;.&quot;,&quot;#FORM.myField#&quot;) GT 0)>
			<CFINCLUDE template=&quot;form_action.cfm&quot;>
			<CFABORT>
		</CFIF>
	</CFIF>
	<CFSET errorMessage = &quot;<P><B>Form error:</B> you must enter a valid email address</P>&quot;>
</CFIF>

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;>[/URL]
<head>
	<title>Form Validator</title>
	<style>
	.requiredLabel{
		color : Red;
	}
	.wingdings{
		font-family : Wingdings;
	}
</style>
</head>

<body>


<b class=&quot;requiredLabel&quot;>Red</b> fields are required.<br />
<CFOUTPUT>
#errorMessage#
<form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; id=&quot;myForm&quot; name=&quot;myForm&quot; method=&quot;post&quot;>
	<b class=&quot;requiredLabel&quot;>Enter your email address:</b><BR />
	<CFIF Len(Trim(errorMessage)) GT 0><span class=&quot;wingdings&quot;><span class=&quot;requiredLabel&quot;>&egrave;</span></span></CFIF> <input type=&quot;text&quot; name=&quot;myField&quot; id=&quot;myField&quot; value=&quot;#FORM.myField#&quot; />
	<P><input type=&quot;submit&quot; name=&quot;submit&quot; id=&quot;submit&quot; value=&quot;Go!&quot; /></P>

</form>
</CFOUTPUT>
</body>
</html>

and form_action.cfm-
Code:
<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;>[/URL]
<head>
	<title>Form Action Page</title>
</head>

<body>

Thank you for submitting the valid email address: <CFOUTPUT>#FORM.myField#</CFOUTPUT>!

</body>
</html>

A couple of things:
You could obviously just include the code for the action page directly in form.cfm, instead of doing a CFINCLUDE... but having them in separate files helps simplify maintenance, particularly if your form is complex.

Also, you could just use a CFELSE, rather than a CFABORT. Good programming practice says that you always refrain from using ABORTs or BREAKs or EXITs... but using a CFABORT in this case allowed me to put the entire IsDefined(&quot;FORM.submit&quot;) block into a custom tag (with a little revision to genericize everything)... so I can share the same validation routines across the entire site.

Finally, doing a CFINCLUDE rather than a CFLOCATION allows the form_action.cfm page access to all the submitted form values. You could use CFLOCATION, but then you'd have to manually build a querystring with all the fieldnames and values. The drawback of using CFINCLUDE is that your URL doesn't chance when the form is submitted... which means your form_action.cfm page won't ever show up in your logs... which means if you run a tracking report with Webtrends or whatever, you won't have an accurate count of how many people actually submitted the form successfully.

Hope it helps,

-Carl
 
Thanks for the info, Carl. The form and its processing take up about 600+ lines of code, and we're not keeping track of how many times the form is being submitted. As a result, I'm just going to include several templates in the original form page. Thanks for the pros/cons information on doing things this way; it'll be useful in the future.

Misty
 
Hi Misty,

I once faced similar problem. Then I tried to put this line in my form_action.cfm:
<meta http-equiv=&quot;REFRESH&quot; content=&quot;3; url=form.cfm&quot;>

Sure, you can change the content value to any value.

<cfif condirion_is_true>
<CFSET Message = &quot;Okay.&quot;>
<meta http-equiv=&quot;REFRESH&quot; content=&quot;0; url=form.cfm&quot;>
<cfelse>
<CFSET Message = &quot;Invalid entry.&quot;>
<meta http-equiv=&quot;REFRESH&quot; content=&quot;3; url=form.cfm&quot;>
</cfif>

Do not forget to put a CFPARAM statement declaring the Message variable.

Hope this helps.
mansii
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top