Reputation: 377
I have an html/javascript page that submits to a Coldfusion page, which consumes the data (writes data to the db), and when the Coldfusion page is done, I want it to resubmit back to the original html/javascript page. In short, I'd like to have the Coldfusion page programmatically trigger a submit event. How do I do this? Note: I can't use Ajax as our environment doesn't allow Cross-Site Scripting.
HTML/JS (MainForm.html):
<form name="myForm" action="processStuff.cfm">
<input type="submit" name="btnSubmit">
</form>
CF:
<cfif IsDefined("Form.btnSubmit")>
. . . Do a bunch of stuff, then submit back to MainForm.html . . .
</cfif>
Upvotes: 1
Views: 813
Reputation: 5500
In a regular cold fusion page, setup in this fashion.
form.cfm
<cfif isDefined("form.submitb") and form.submitb is "yes">
... submit form
<cfelse>
... show form ...
</cfif>
You can do this
otherpage.cfm
<cfset form.submitb = "yes" />
<cfset form.firstname = "John" />
<cfset form.lastname = "Doe" />
<cfinclude template="form.cfm">
If you're using a recent version of CF/Railo, you can...
<cfset StructAppend(form, {submitb = "yes", firstname = "John", lastname = "Doe"}, false) />
where the false
is the value of OverWriteFlag, which specifies whether the values of the second struct will overwrite the values of the first if a conflict occurs.
Note that we're not assigning the value of the StructAppend to a variable like <cfset form = StructAppend{....}>
. Doing so would set the value of form to true
, which would be bad. Some other functions like ArrayAppend()
work in the same fashion.
We could also just say
<cfset form = {....}>
but you may not want to erase and overwrite otherpage.cfm's form data.
Finally, when a form is submitted, Cold Fusion generates a field called form.fieldnames
that contains field names of all the form elements in your form. This is not generated when you add to the form scope programmatically.
<cfset form.newfield = "test" />
adds newfield
to the form scope but it does not add newfield
to the list of form.fieldnames. The workaround for this is (if you need it)
<cfset ffnames = StructKeyList(form) />
<cfif listfind(ffnames,"fieldnames") />
<cfset ffnames = ListDeleteAt(ffnames,ListFind(ffnames,"fieldnames")) />
</cfif>
Upvotes: 1
Reputation: 11120
There are lots of ways to do this. I like separating the behavior based on whether it was a get
or post
.
<cfif cgi.request_method EQ "post">
<cfquery>
INSERT INTO formResults(name)
VALUES(<cfqueryparam value="#form.name#" cfsqltype="CF_SQL_VARCHAR">)
</cfquery>
</cfif>
<form method="POST">
Name: <input type="text" name="name">
<input type="submit">
</form>
Upvotes: 0
Reputation: 1549
It really all depends upon your code. Are you using a framework? Are you using CFC's. Are you using purely procedural code? etc.
But for simplicity, let's say you're doing procedural code and incorporate you model and view together.
Just add conditional logic on the same .CFM file you are using to display your form. Then your form submits back to the same page.
myForm.cfm
<html><body>
<cfif structKeyExists(form, "btnSubmit")>
<cfquery>
INSERT INTO formResults(name)
VALUES(<cfqueryparam value="#form.name#" cfsqltype="CF_SQL_VARCHAR">)
</cfquery>
</cfif>
<form method="POST">
Name: <input type="text" name="name">
<input type="submit" name="btnSubmit">
</form>
</body></html>
Upvotes: 3
Reputation: 14764
Perhaps what you want is to redirect back to the form? If so, just use cflocation
.
<cfif isDefined("form.btnSubmit")>
. . . Do a bunch of stuff, then submit back to MainForm.html . . .
<cflocation url="MainForm.html" />
</cfif>
Upvotes: 4