Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Submit form and execute ColdFusion function?

Guest
Oct 10, 2007 Oct 10, 2007
Hello!

I have a coldfusion form. What I need to be able to do is submit the form and have it execute a ColdFusion function that I have defined. Can this be done? If so, how?

I know it can be done with a JavaScript function.

Thanks!
3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 10, 2007 Oct 10, 2007
<cfscript>
function saveResults () {
<cfset gotourl = "saveresults.cfm/link?query=" & sql>
<cflocation url = gotourl>
}
</cfscript>

<cfform name="saveresults"
action=" http://pluto2/brandi/serverstats/saveresults.cfm" method="post">
<input type="submit" name="Submit" value="Save results as Excel
spreadsheet">
</cfform>

Your function serves no purpose as I understand your requirements. The
action of the form will determine what resource the submit action
requests. Do to the stateless...
Translate
LEGEND ,
Oct 10, 2007 Oct 10, 2007
"I have a coldfusion form. What I need to be able to do is submit the
form and have it execute a ColdFusion function that I have defined. Can
this be done? If so, how?

I know it can be done with a JavaScript function."

Yes, if you in someway send a request to the server to run the CF
function. CFML runs on the server and JavaScript runs on the client and
never the two will meet. Using modern techniques such as AJAX one can
make it look to the user that they are, but under the hood CFML is
always confined to the server and JavaScript the client.

More details on what you are trying to do would help.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 10, 2007 Oct 10, 2007
This page displays a query that is sent via the url.

The form at the bottom contains a button that, when clicked, I want to execute the saveResults() function that re-directs to another coldfusion page where I have code that will save the query results as an xml file (which you so graciously assisted me with earlier today 😃 ).

Or, perhaps, there is a much easier way of doing this and I am just creating too much work. Any suggestions are appreciated.

Code below...

<cfscript> setEncoding("URL", "Shift_JIS"); </cfscript>
<cfset sql = URL.query>
<cfoutput>#sql#</cfoutput>

<cfquery name = "query" datasource = "site"> #replaceNoCase(sql, "''", "'", "all")# </cfquery>

<cfdump var="#query#"></cfdump>

<cfscript>
function saveResults () {
<cfset gotourl = "saveresults.cfm/link?query=" & sql>
<cflocation url = gotourl>
}
</cfscript>

<cfform name="saveresults" action="" method="post">
<input type="submit" name="Submit" value="Save results as Excel spreadsheet">
</cfform>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 10, 2007 Oct 10, 2007
<cfscript>
function saveResults () {
<cfset gotourl = "saveresults.cfm/link?query=" & sql>
<cflocation url = gotourl>
}
</cfscript>

<cfform name="saveresults"
action=" http://pluto2/brandi/serverstats/saveresults.cfm" method="post">
<input type="submit" name="Submit" value="Save results as Excel
spreadsheet">
</cfform>

Your function serves no purpose as I understand your requirements. The
action of the form will determine what resource the submit action
requests. Do to the stateless nature of HTTP reqeusts/ & resposnes,
that resource will have no information from this request unless you do
something to pass it along.

The solution that requires the smallest change to your code is simply
put the appropriate information from the function into the form action.

<cfoutput>
<cfform name="saveresults"
action=" http://pluto2/brandi/serverstats/saveresults.cfm/link?query=#sql#"
method="post">
<input type="submit" name="Submit" value="Save results as Excel
spreadsheet">
</cfform>
</cfoutput>

If you are open to more significant modification to your code, it could
probably be simplified and more preformant with the use of persistent
scopes such as session and|or application to store the results and|or
the sql data so that the second request that uses the exact same data
does not have to regenerate it.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 11, 2007 Oct 11, 2007
Thanks Ian!

I am open to anything that makes my code better and more efficient - I'm no CF expert as I'm sure you can tell so help and advice is always appreciated.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 11, 2007 Oct 11, 2007
Now here's some interesting behavior...

I made the changes you suggested:
<cfform name="saveresults" action=" http://pluto2/brandi/serverstats/saveresults.cfm/link?query=#sql#" method="post">
<input type="submit" name="Submit" value="Save results as Excel spreadsheet">
</cfform>

But when it redirects I the page displays '500 null'.

I tried commenting out the piece of code in results.cfm that gets the data from the url and directing it only to saveresults.cfm:
<cfform name="saveresults" action=" http://pluto2/brandi/serverstats/saveresults.cfm" method="post">

and it worked just fine.

Any idea what might be causing this?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 11, 2007 Oct 11, 2007
LATEST
Any idea what might be causing this?

I should have thought about the kind of data you are sending through the
URL. A complex string such as a sql statement would need to be encoded
to pass through a URL.

" http://pluto2/brandi/serverstats/saveresults.cfm/link?query=#URLEncodedFormat(sql)#"



I would have approached this problem form a different angle. I would
run the query and store the results in a session variable. Then I could
just go to the 'saveresults.cfm' page and access the results from the
session scope and not have to pass the sql statement through or run the
query again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources