4

I am using ColdFusion function shown below:

<cffunction name="getdata" access="remote" output="true" returntype="string">
    <cfargument name="x" type="string" required="true">
    <cfargument name="y" type="string" required="true">
    <cfquery name="getval" datasource="#application.datasource#" result="result">
    select val from table where col1=#arguments.x# and col2=#arguments.y#
    </cfquery>

    <cfset p= #getval.val#>
    <cfreturn p>
</cffunction>

I want the value of val to be printed in an alert. Alert displayed should be "please verify whether it's val" when user selects one of the values in autocomplete drop down.

How can I achieve this?

I tried using jQuery AJAX inside select event in autocomplete, here x and y are the names of input fields on the form:

 select: function(event, ui) {
     $.ajax({
            type: "GET",
            url: 'somepathtofunction?method=getdata&x='+ x + '&y'+ y,
            async: true,
            success: function(result) {
                alert('please verify whether it is' + result.val  );
            }
     });
}             

Also, tried the code below:

alert('please verify whether it is'  +  $.get('somepathtofunction?method=getdata&x='+ x + '&y'+ y));

but none of these worked.

I am new to jQuery AJAX and ColdFusion so I know I have written most of the wrong code. Kindly correct me wherever I am wrong.

2
  • 1
    Not related to your question, but in ColdFusion functions it is usually desirable to keep ColdFusion variables inside the function. This can be done with the var keyword or by using the local scope. Commented Jul 30, 2020 at 20:21
  • Not to mention using <cfqueryparam> especially when dealing with variables that come from the request. Commented Jul 30, 2020 at 20:33

1 Answer 1

1

You're on the right path with the AJAX call. There are a multitude of ways to pull this off. I have found one of the simplest ways to have both CF and JS use JSON notation. Your CF function should be set to output=true and use the #serializeJSON()# function to return the data.

One 'gotcha' to note is CF always coapitalizes JSON data by default so you have to use the ALL CAPS version of your return variables in the case-sensitive JS code.

Here's something to try:

<cffunction name="getdata" access="remote" output="true">
    <cfargument name="x" type="string" required="true">
    <cfargument name="y" type="string" required="true">
    <cfquery name="getval" datasource="#application.datasource#" result="result">
    select val from table where col1=#arguments.x# and col2=#arguments.y#
    </cfquery>
    <cfoutput>#serializeJSON({val=getval.val})#</cfoutput>
</cffunction>




  $.ajax({
        method: "GET",
        url: "yourCFCpathhere.cfc",
        data:{
            method: "getdata",
            x: var1,
            y: var2
        },
        dataType: "json"
    }).done(function( result ) {
        alert('please verify whether it is' + result.VAL  ); //CAPS for the return var from CF
    });
});
Sign up to request clarification or add additional context in comments.

15 Comments

Why would you output the data instead of returning it with a cfreturn tag.
Why use result="result"? And those arguments need to be in <cfqueryparam>
that code worked though. Thanks much @P Mascari. How can do it without using result="result". And how can return it using cfreturn @JamesA Mohler?? I may sound like stupid but please explain I am really new to this.
Thanks @Dan Bracuk. Also, I have two input fields on the form and user can select options from the autosuggestion dropdown. I wanted to pass that value selected at runtime by the user to x and y. I did this by doing x="abc" but I dont want that. I want x= "value from the input text box".
This link shows one way to use the cfreturn tag to pass json data back to javascript stackoverflow.com/questions/40406253/…. This link shows how to pass form values to javascript - stackoverflow.com/questions/3547035/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.