Skip to main content
March 29, 2011
Answered

Using <cfajaxproxy> to handle form submission?

  • March 29, 2011
  • 1 reply
  • 746 views

Coldfusion 9

Im trying to submit a <cfform> to a cfc function without the calling cfm refreshing. The form name is dynamic, and so are the elements within the form. The javascript seems to pass the form as a structure to my cfc, but the <select> value and <textarea> are empty.

------------------------------------------------------------------------------------------------------------------

<cfform id="#randomID#" method="post">

<cfselect name="firstElement">

<option name="firstOption" value='firstValue">first option</option>

<option name="secondOption" value="secondValue">second option</option>

</cfselect>

<cftextarea type="text" name="textAreaOne" rows="5" cols="45"></cftextarea>

<cfinput name="submit" type="button" value="Send" onClick="sendForm('#randomID#')">

</cfform

This is the javascript function that is called by the form:

(The .cfc is formAction.cfc)

------------------------------------------------------------------------------------------------------------------

<cfajaxproxy cfc = "formAction" jsclassname = "cfcClass">

<script type="text/javascript" language="javascript">

function sendForm(formID){

var cfc = new cfcClass();

//This seems to send the form as a structure to my cfc, but the <select> value and <textarea> are empty

var result = cfc.send(formID);

}

This is the CFC:

------------------------------------------------------------------------------------------------------------------

<cfcomponent>
<cffunction name="send" access="remote" returntype="string">
<cfargument name="formResults" type="struct" required="yes">


<cfreturn>
</cffunction>
</cfcomponent>

------------------------------------------------------------------------------------------------------------------

My goal here is to pass the form to the cfc as a structure. I can then loop through the structure and create a list of both the keys and the values. I've also tried to create a string of the values within the javascript prior to the cfc call, but ive run into problems as so much of each form is dynamic.

------------------------------------------------------------------------------------------------------------------

Here is my attempt to extract the form values prior to the cfc call:

function sendForm(formID){

//loop through the form in question

for(i=0; i<document.getElementById(formID).elements.length; i++){

//this works for simple imput elements but not for <select>

var value = document.getElementById(formID).value;

//for <select> ive tried this, but the selectedIndex is always 0:

var value =document.getElementById(formID).selectedIndex;

------------------------------------------------------------------------------------------------------------------

If anyone could help me with either method; send the complete form structure to the cfc, or create a list of values to send to the cfc, i would be a very happy man.

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    There is an easier way to submit to your cfc without the main page refreshing.  Submit your form to a .cfm page in a very small iframe.  Then that .cfm file can send the form as a structure to your cfc.

    1 reply

    Dan_BracukCorrect answer
    Inspiring
    March 29, 2011

    There is an easier way to submit to your cfc without the main page refreshing.  Submit your form to a .cfm page in a very small iframe.  Then that .cfm file can send the form as a structure to your cfc.