Creating forms that process themselves
So far, the action attribute in our forms have pointed to a different template.
In this example the form action attribute will pointed to the same template as our form.
This can sometimes be useful to reduce your workload of maintaining several files.
To process a form to the same template, simply check for the existence of the fieldname variable in the form scope. If the template has not been posted, neither the form scope nor the fieldname variable will exist. Therefore, the form will be displayed.
Instead of using CGI.SCRIPT_NAME, you can also using Always get the correct URL path.
<!--- Create a variable to check whether this form has been posted. --->
<!--- In this example we assumes that the form has not been posted. --->
<cfparam name="Posted" default="No">
<!--- Check to see whether the form scope exists. --->
<!--- If Yes: the form has been posted, if not the <cfelse> will be started. --->
<cfif IsDefined("Form.Your_Name")>
<!--- Set the Posted variable to Yes. --->
<cfset Posted = "Yes">
<!--- Reassign the form field values to local variables. --->
<cfset Your_Name = Form.Your_Name>
</cfif>
<!--- The form has been posted, process the form data. --->
<cfif Posted>
You have allerede posted the form: <cfoutput>#Your_Name#</cfoutput>
<!--- The form has not been posted, process the form. --->
<cfelse>
<!--- Notice: the CGI.SCRIPT_NAME contains the name of your page. --->
<form action="<cfoutput>#CGI.SCRIPT_NAME#</cfoutput>" method="post" name="example" id="example">
Your Name
<input type="text" name="Your_Name" />
<input name="submit" type="submit" value="Post" />
</form>
</cfif>
Advertisement
User Comments: 1
New way to the same thing
<cfif isDefined("submit")>
<cfoutput>#nickname#</cfoutput>
<cfelse>
<form name="form" method="post" action="index.cfm">
Nick Name : <input name="nickname" type="text" size="20" maxlength="20"><br>
<input name="submit" type="submit" value="Result">
</form>
</cfif>


