Can I use ColdFusion tags in JavaScript?

半世苍凉 提交于 2020-01-11 10:26:26

问题


Can I use ColdFusion tags in JavaScript? For example:

 <script language="javascript" type="text/javascript">
   function validateUser() {
    var userName = document.getElementById("username");

 <CFQUERY DATASOURCE="mydatasourcename" NAME="getUser">
  select USER_ID,COUNT(*) from  user u 
 where u.firstname=userName;
  </CFQUERY>
 <cfif getUser.recordCount EQ 0>
   <!--- Show eroor message --->
   <cfelse>
    <!--- Assign userId to hidden field --->
    document.getElementById("userid").value=#USER_ID#
  </cfif>   
    }
 </script>

<input type='textbox' name='username' id='username' onblur=validateUser()/>
<input type='hidden' name='userid' id='userid'/>

When the end user enters their username, I would like to check in a database if this username exists or not. If it exists, I have to keep the userid in the hiddenfield or else throw an error.

Am I doing this correctly? If it is wrong, could you suggest the correct way?


回答1:


Long version: http://blog.adamcameron.me/2012/10/the-coldfusion-requestresponse-process.html

Short version: no, you're not doing it right.

Mid-sized StackOverflow-friendly version: CFML code runs on the server side of a request; JavaScript runs on the client browser. And to be clear: the ColdFusion server never communicates with the browser directly at all: there's a web server in between. The client browser requests a file, the web server is configured to pass .cfm requests to the ColdFusion server, and it runs its code, returning the resulting string (eg: an HTML web page) to the web server which then returns that to the browser. That HTML might include JavaScript (inline or as external requests) which the browser will then execute.

Hopefully from that you can see that there's no direct interaction between server-side code and client-side code.

You have two facilities at your disposal to get the two communicating asynchronously though. Firstly: CFML code writes out text, but that text can be JS which the browser then runs when it finally receives it. Something like:

<cfset msg ="G'day world">
<script>alert("<cfoutput>#msg#</cfoutput>");</script>

Once the CFML server has processed that, what gets sent back to the browser is:

<script>alert("G'day world");</script>

In this way server-side code data can be used in client-side process if the server-side code "writes out" the data as part of its response. The example above is very trivial and not a "good practice" way of going about this, but it demonstrates the technique.

If you need to use JS code on the client to communicate back with the server, your only (real) recourse is to make an AJAX request back to the server to pass it client-side information for further server-side processing and for the server to respond with something. It is outwith the scope of your question to explain how best to do this, but there is a tonne of information out there to do this.

CFML provides some "wizards" to write HTML and JS out for you to facilitate this, but on the whole this is a bad approach to achieving this end, so I will not recommend it. However I will point you to a project which offers HTML/JS/CSS solutions to the inbuilt CFML wizardry: https://github.com/cfjedimaster/ColdFusion-UI-the-Right-Way

Back to the short answer: no, you cannot do what you are setting out to do for very good reasons, but if you revise your approach, you can achieve the ends that you want.

What you need to look at is passing the form fields back to the server via AJAX (jQuery makes this very easy), and run your <cfquery> code in a separate request.

If you read that blog article I mention from the outset (discloure: I wrote it, but I wrote it specifically for situations like this), then you'll understand why.

If you get stuck when working on part of your solution: raise another question more focused on whatever part you are stuck on.



来源:https://stackoverflow.com/questions/35417317/can-i-use-coldfusion-tags-in-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!