How can I make a form submit to itself without hardcoding the file name?
The ColdFusion CGI variable CGI.SCRIPT_NAME contains the name of page being executed, so you can use:
Note the use of <cfoutput> around the form tag. This is required unless you are using <cfform>.
This question was written by Ben Forta.
It was last updated on January 9, 2006 at 7:09:24 AM EST.
CFML Referenced
Categories
Comments
Comment made by Phillip Senn on January 9, 2006 at 8:37 AM
There was some discussion at http://ray.camdenfamily.com/index.cfm/2005/10/4/ColdFusion-Contest-Entry-Examined about using <cfset VARIABLES.thisPage = GetFileFromPath(GetTemplatePath())> <form action="#thispage#" ...> instead. Something about running on a context root other than "/".
Comment made by Raymond Camden on January 9, 2006 at 9:15 AM
True - but I think I feel ok about this tip, since it is, in general, safe to use. Your comment alone could serve as a good warning I think.
Btw - your comment fleshed out an error in the site. Thanks. :)
Comment made by Patrick Correia on January 9, 2006 at 11:38 AM
I've always just used a blank action attribute, i.e. <form action="" method="post"> to make the form submit to itself. Is there a problem with this approach that I'm not aware of?
Comment made by Kevin Favro on January 9, 2006 at 3:38 PM
If you have variables passed in your query string and you need those passed as well when the form submits to itself, you can do so like this:
<form action="#cgi.SCRIPT_NAME#<cfif Len(cgi.QUERY_STRING)>?#cgi.QUERY_STRING#</cfif>">
Comment made by Rick Faircloth on January 11, 2006 at 11:14 PM
What's the problem with hardcoding the page name as the form action target?
Comment made by Raymond Camden on January 12, 2006 at 9:38 AM
The problem occurs if you rename the file and forget to update the form. Also - the form may be included into other files. Keeping the action dynamic will make updates easier.
Comment made by Michael on March 11, 2006 at 7:34 PM
Lets do this the easy way!
Try <cfform> thats it!
It passes back the url variables.. in every browser, and have never had a problem with this. I consider this action to be my default action for all forms I design. It makes things so simple.