Link to request CF request in JS?

How to reference the result of a coldfusion request to a javascript function?

<CFQUERY name ="getPin" datasource = "pins"> SELECT pin FROM pinuser where email="#getEmail#" </cfquery> 

I want to set pin as a javascript variable called pincode , I tried:

 var <cfoutput>#ToScript(#pin#,"pincode")#;</cfoutput> 

then

 alert(pincode) 

but it doesn’t work, any ideas?

+4
source share
2 answers

This should work:

 <script type="javascript" language="text/javascript"> <cfoutput> var pincode = '#getPin.pin#'; alert(pincode); </cfoutput> </script> 

I would also include "top 1" in SQL or maxrows = 1 in the cfquery tag, unless that is the main key of the table or otherwise has a unique constraint.

+1
source

It is really simple and useful:

 <script> <cfoutput>#toscript(getPin.pin, "JSVariableName")#</cfoutput> </script> 

Your output should be something like this:

 <script> var JSVariableName = 12345; // </script> 
+3
source

Source: https://habr.com/ru/post/1401421/


All Articles