Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript function within Cold Fusion

Status
Not open for further replies.

jl8789

MIS
May 22, 2003
293
0
0
US
Let's say I have a javascript function that displays an alert message, like:
<script type="text/javascript" language="JavaScript">
function SONSFX_Combo(status) {

if (confirm("That Son and SFX Combination already exist. Do you want to save your new order anyway?")) {
return true;
}
else {
return false;
}
}
</script>

Then in my cold fusion code, I call it:
<CFIF #COUN2# GTE 1>
<script language="JavaScript">SONSFX_Combo();
</script>

<!--- <FONT class="redheader">SONFSX Error</font><br>--->
<FONT class="redheader">SONFSX Error</font><br>
<cfset errors=errors+1>
</CFIF>

How do I check for the return value of true or false from the alert message? AND how could I simply set the errors variable to errors=errors+1 if it is false, and errors=0 if it is true? Can I set the cold fusion variable within the javascript?
 
You can't get javascript to set coldfusion var. because the coldfusion part/code is run before the html page is created and send to the browser. If you want to get interaction you have to submit the page with the values.

Erwin Oosterhoorn
Analyst Programmer,
ice hockey player/fan.
 
See it this way.
When you go to a restaurant and order a dish you get everything already cooked and 'served'. Once is in your table you can condiment with whatever you want. You can either eat it with a fork, chopsticks or your hands. At this point the chef doesn't really care how you eat it. On the other hand you don't care about all the ingredients and the amount of them used in your meal, you only care that is good and edible.

Coldfusion is your chef, it works behind the scenes and once the code gets served it doesn't have any interaction with Javascript. On the other side Javascript doesn't talk to ColdFusion directly because they work in different places at different times. (CFML runs first serves the pages and the JS can interact with the HTML page served).

The only way to get your meal changed (you got a raw steak and you want it well done) is to send back the whole dish back to the chef.

Can anybody tell if I'm hungry???

grtfercho çB^]\..
"Imagination is more important than Knowledge"A. Einstein
 
The only way you can really send JS variables back to CF is to send it in the url. example.

<script type="text/javascript" language="JavaScript">
function SONSFX_Combo(status) {

if (confirm("That Son and SFX Combination already exist. Do you want to save your new order anyway?")) {
window.location = 'thispage.cfm?order=confirm';
}
else {
window.location = 'thispage.cfm?order=cancel;
}
}
</script>

then in your cfm page you can process on the value of #url.order#
<cfif url.order eq "comfirm">
...process order...
<cfelse>
<cflocation url = "thanksAnyWay.cfm">
</cfif>

now if you wanted to put a CF variable in JS you could do it like this.

<script language = "javaScript">
function showAlert(msg){
alert(msg);
}
</script>
<input type = "button" value = "click me to see a message" onclick = "showAlert(<cfoutput>#url.message#</cfoutput>)">

that assumes you passed a url variable that contains a message like this:
mypage.cfm?message=I am a message from a url
granted that's a pretty stupid example but it's handy for errors and stuff if you put the alert in the head without a function so it runs automaticaly.


Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.
-Douglas Adams (1952-2001)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top