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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to return Form Variables back to calling Forms Page

Status
Not open for further replies.

attick111

Programmer
Nov 28, 2001
21
US
I have a Form page (registform1.cfm) which calls an action page called 'processme.cfm'. This page then first checks to make sure the username entered doesn't already exist in the database. if it does, I use the <CFLOCATION> to return the already filled in fullname.
<CFIF Checkusername.RecordCount GT 0>
<CFLOCATION URL=&quot;RegistForm1.cfm?Message=#URLEncodedFormat(&quot;The
username you chose already exists, please choose a different
username.&quot;)#&FullName=#URLEncodedFormat(FullName)#&quot;>
</CFIF>

Also, it checks whether the company_number entered is a valid one in the database. However, I want the ablility when this happens to send back all the other Form fields (Form variables) minus the username i.e the company_code, last_name, first_name, address, email, etc without having the user re-enter all this informaton again. At least for now I am able to send back the fullname. For now, my fields are blanked out when I am redirected back to the registration form page.

Can someone help me out here with the right way to do this. I'm new to Coldfusion and will appreciate any help.

Attick111
 
Well... you could pretty easily append all the form variables to your URL line in the CFLOCATION:
Code:
<CFIF Checkusername.RecordCount GT 0>
  <CFSET myQueryStr = &quot;&quot;>
  <CFLOOP index=&quot;whichFormField&quot; list=&quot;#FORM.FieldNames#&quot;>
     <CFIF CompareNoCase(whichFormField,&quot;username&quot;) NEQ 0>
         <CFSET myQueryStr = myQueryStr & &quot;&&quot; & whichFormField & &quot;=&quot; & URLEncodedFormat(FORM[whichFormField])>
     </CFIF>
  </CFLOOP>
  <CFLOCATION URL=&quot;RegistForm1.cfm?Message=#URLEncodedFormat(&quot;The
              username you chose already exists, please choose a different
              username.&quot;)#&FullName=#URLEncodedFormat(FullName)##myQueryStr#&quot;>
</CFIF>

But... with a complex form (or even with a simple form), you're likely to overrun the limit to the amount of characters that are supposed to be in a URL (which I believe is 255).


Let me turn you around a little bit. Your solution is the reverse of what I've always implemented. Instead of the form submitting to an different action page, have it submit to itself... then either do a CFLOCATION to a thank you page or CFINCLUDE the thank you page at the top (if you need access to the form variables).

Try copying and pasting the following simple example:
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]
<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;>[/URL]
<head>
	<title>Form Submission</title>
</head>

<body>
<CFPARAM name=&quot;FORM.firstname&quot; default=&quot;&quot;>
<CFPARAM name=&quot;FORM.lastname&quot; default=&quot;&quot;>
<CFPARAM name=&quot;FORM.zipcode&quot; default=&quot;&quot;>

<CFIF IsDefined(&quot;form.submit&quot;) AND CompareNoCase(form.submit,&quot;Go&quot;) EQ 0>

	<CFIF Len(Trim(FORM.firstname)) GT 0 AND Len(Trim(FORM.lastname)) GT 0>
		<CFINCLUDE template=&quot;thankyou.cfm&quot;>
		<CFABORT>
		
	<CFELSE>
	    <p>There was an error in your form.<br />
		Kindly fill in all the required fields.</p>
	</CFIF>

</CFIF>

<p>&nbsp;</p>
* = required fields<br />
<CFOUTPUT>
<form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; method=&quot;post&quot;>
First name *: <input type=&quot;text&quot; name=&quot;firstname&quot; value=&quot;#FORM.firstname#&quot; /><br />
Last name *: <input type=&quot;text&quot; name=&quot;lastname&quot; value=&quot;#FORM.lastname#&quot; /><br />
Zip code (optional): <input type=&quot;text&quot; name=&quot;zipcode&quot; value=&quot;#FORM.zipcode#&quot; /><br />
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Go&quot; />
</form>
</CFOUTPUT>

</body>
</html>

and
thankyou.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]
<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;>[/URL]
<head>
	<title>Form Thank You</title>
</head>

<body>

<CFOUTPUT>Thank you, #FORM.firstname# #FORM.lastname# for filling out our form correctly!</CFOUTPUT>

</body>
</html>

What the form.cfm page does is set up a form to submit to itself (#GetFileFromPath(GetBaseTemplatePath())# is a fancy way to get the present filename... so if you ever happen to rename or copy your form page, you'll always be submitting to the right place without having to touch the code).

When the form is submitting (to itself), the top of the form.cfm page recognizes that it's been submitted, checks that all the required fields are filled in, then includes the thank you page and aborts the redisplay of the form. If all the fields aren't filled in, it simply doesn't include the thank you page, doesn't abort, and falls out to redisplay the form again (with the submitted data already filled in).

It's a very handy method to use... and tends to be much easier to maintain than passing variables back on the URL or whatnot.

-Carl
 
Thanks a lot Carl for your Help !!!!!

Attick111
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top