Solved

Returning a variable after using ColdFusion.Ajax.submitForm and a CFC

Posted on 2009-03-30
4
666 Views
Last Modified: 2013-12-24
I am new to using ajax and also not very saavy with Javascript either.  I have a form that I want to submit using the ColdFusion.Ajax.submitForm function and replace the form with a simple text message saying "Thank you!". I'll include clips from both my form page and also the cfc that's being called.  The data is inserting, but I can't get a response to return (at least not in a format I can recognize. Thanks!
head of form page...
 
<script type="text/javascript">
          function doInsertStats() {
 
        ColdFusion.Ajax.submitForm('insertForm', 'queries.cfc?method=insertstats');
              
      }
</script>
 
***************************************************************************************************
 
 
function in the queries.cfc page...
 
<cfcomponent displayname="Queries">
 
	<cffunction name="insertstats" access="remote" output="false" returntype="string">
	
 
	      <cfargument name="formname">
	      <cfargument name="formname">
	      <cfargument name="formname">
                     
 	            
                  <cfquery name="InsertStats" datasource="#datasource#">
            
insert into ExampleTbl( column1, column2,  column3 )
	            values (
	
	                  <cfqueryparam cfsqltype="cf_sql_varchar" value="#formvalue#">,
	                  <cfqueryparam cfsqltype="cf_sql_varchar" value="# formvalue#">,
                                        <cfqueryparam cfsqltype="cf_sql_varchar" value="# formvalue#">
 
	                        )
				
	cfset ExampleComplete = "1">	
 
	</cfquery>
	
	   <cfreturn ExampleComplete>
	      
	</cffunction>
	
</cfcomponent>

Open in new window

0
Comment
Question by:scripkilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 24022418
First thing why  are you keeping <cfset> statement in <cfquery>  remove that you don't need


<cfset statement at all

second thing

ColdFusion.Ajax.submitForm has two more parameters which are java script  functions , first one which executes if there are no erros and second if there are erros.

see the below examples.


  arguments are form name, cfc and method to send form values to, javascript function to handle result,
       javascript function to handle error
      */
      ColdFusion.Ajax.submitForm('insertForm', 'employeeService.cfc?method=insertData', resultInsertHandler, insertErrorHandler);

so here your statement will become like following

 function doInsertStats() {
 
        ColdFusion.Ajax.submitForm('insertForm', 'queries.cfc?method=insertstats',resultInsertHandler, insertErrorHandler);
             
      }




function resultInsertHandler(text) {

     
        var message ="Thank you!";
        var resultDiv = document.getElementById("result");
       
       
        resultDiv.innerHTML = "<p>"+message+".</p>";
               
   
       
}

   
    function insertErrorHandler(id, message) {


        var resultDiv = document.getElementById("result");
       
        resultDiv.innerHTML = "<p>Unsuccessfull</p>";
   
    }
   

at the bottom of ur code where u want to dipaly message create a div with id result

<div id = "result">

</div>


or else you can keep just alert statements in respective jaav script functions let me know if you need furthur help

0
 
LVL 13

Accepted Solution

by:
srikanthmadishetti earned 125 total points
ID: 24022668
Change your code like this


head of form page...
 
<script type="text/javascript">
          function doInsertStats() {
 
        ColdFusion.Ajax.submitForm('insertForm', 'queries.cfc?method=insertstats',resultInsertHandler, insertErrorHandler);
             
      }
     
      function resultInsertHandler() {
   

        var resultDiv = document.getElementById("result");
       
        resultDiv.innerHTML = "<p>Thank you!.</p>";
       
       

   
    }
   
   
    function insertErrorHandler(id, message) {
   
       alert("Error while updating\n Error code: "+id+"\n Message: "+message);

        var resultDiv = document.getElementById("result");
       
        resultDiv.innerHTML = "<p>Insert unsuccesfull.</p>";
   
    }
   

   
</script>
 
***************************************************************************************************
 
 
function in the queries.cfc page...
 
<cfcomponent displayname="Queries">
 
        <cffunction name="insertstats" access="remote" output="false"  returntype="void">
       
 
              <cfargument name="formname">
              <cfargument name="formname">
              <cfargument name="formname">
                     
                   
                  <cfquery name="InsertStats" datasource="#datasource#">
           
insert into ExampleTbl( column1, column2,  column3 )
                    values (
       
                          <cfqueryparam cfsqltype="cf_sql_varchar" value="#formvalue#">,
                          <cfqueryparam cfsqltype="cf_sql_varchar" value="# formvalue#">,
                                        <cfqueryparam cfsqltype="cf_sql_varchar" value="# formvalue#">
 
                                )
                               
       
 
        </cfquery>
       
         
             
        </cffunction>
       
</cfcomponent>




<!---add this in u form page  to display result --->
    <div id="result">
    </div>


0
 
LVL 1

Author Comment

by:scripkilla
ID: 24029412
I think we are really close, thank you for explaining it so well.  When I click on the form submit button, I am getting the Object Expected JS error on line 286.  This is the that calls the function...

<input type="button" value="Submit" onclick="DoInsertStats()" >

any thoughts?
0
 
LVL 1

Author Comment

by:scripkilla
ID: 24029439
Nevermind, I had an uppercase 'D'oInsertStats() - problem solved.  Thanks!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Server specifications for web hosting 7 96
Web Site Hosting 10 100
Nameserver and MX Record 2 83
what is socket pooling? 8 79
A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question