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

ColdFusion Self-Submitting Forms? 1

Status
Not open for further replies.

rockyroad

Programmer
Feb 22, 2003
191
US
Hello,

Can anyone illustrate clearly the method for self-submitting a form, such that the form submission and processing are done on one template, (standard HTML forms and/or <CFFORM>? I know it must be relatively easy but I have some kind of mental block against it, having learned CF using exclusively 2-template form processing.

THANKS!!!!

rr
 
Add after closing the form tag:
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
     document.forms[0].submit();
</SCRIPT>
DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Hi mate,

If I understand your question correctly then you need the following:

Code:
<cfif isDefined(&quot;process_it&quot;)>

Code to process form here

<cfelse>

Form code here

Make sure the following field is in your form.

<input type=&quot;hidden&quot; name=&quot;process_it&quot; value=&quot;1&quot;>

</cfif>

Hope this helps Wullie


The pessimist complains about the wind. The optimist expects it to change.
The leader adjusts the sails. - John Maxwell
 
Okay... I thought I understood your question, RockyRoad, but after reading the responses I think maybe I'm missing something.

You're asking how to set up a single CFM page with a form so that it submits to itself, and, following that, how to tell that the form's been submitted so you can act on it, right?

If so, as you say, it's relatively easy. The key is realizing that the value of the submit button is passed as one of the form variables... just like everything else on the form.

So you would set up something like:

Code:
<!--- set up some CFPARAMs so we can have some defaults --->
<CFPARAM name=&quot;FORM.firstname&quot; default=&quot;&quot;>
<CFPARAM name=&quot;FORM.lastname&quot; default=&quot;&quot;>

<CFIF IsDefined(&quot;FORM.submit&quot;) AND CompareNoCase(FORM.submit,&quot;Do it!&quot;) EQ 0>
   <!--- the form was submitted, act on it however you like, including doing selects, inserts, or just CFOUTPUTs --->

   Thank you, <CFOUTPUT>#FORM.firstname# #FORM.lastname#</CFOUTPUT> for submitting the form!

</CFIF>

<CFOUTPUT>
<form action=&quot;#GetFileFromPath(GetBaseTemplatePath())#&quot; method=&quot;POST&quot;>
    <p>First name:<br />
    <!--- include the most recent value of the submitted form, if you like, by simply setting the value to the variable --->
    <input type=&quot;text&quot; name=&quot;firstname&quot; value=&quot;#FORM.firstname#&quot;></p>
    <p>Last name:<br />
    <input type=&quot;text&quot; name=&quot;lastname&quot; value=&quot;#FORM.lastname#&quot;></p>

    <p><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Do it!&quot;></p>
</form>
</CFOUTPUT>

The above redisplays the form when it's been submitted. This is one of the chief benefits of submitting to itself. But, if you don't want the form to display again, you can always replace the </CFIF> with a <CFELSE> so that the form is wrapped in a block that would only execute if the form hasn't been submitted yet.


Is that what you had in mind?
-Carl
 
Also here is a way of getting the select option to be selected when it comes back to the page.

to test:
<cfparam name=&quot;Form.selectit&quot; default=&quot;This&quot;>
<cfset optionlist=&quot;This,That,Whatever&quot;>
<cfset sel=&quot;&quot;>
<html>
<head>
<title>Select Option</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;test.cfm&quot;>
<table width=&quot;309&quot; height=&quot;23&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;>
<tr>
<td width=&quot;199&quot; align=&quot;right&quot;>Select Something:</td>
<td width=&quot;110&quot;>
<select name=&quot;selectit&quot;>
<cfloop index=&quot;opt&quot; list=&quot;#OptionList#&quot; delimiters=&quot;,&quot;>
<cfif opt is Form.SelectIt><cfset sel = &quot;selected&quot;></cfif>
<cfoutput><option #sel#>#opt#</option></cfoutput>
<cfset sel=&quot;&quot;>
</cfloop>
</select>
</td>
</tr>
</table>
<p>
<input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</p>
</form>
</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top