Posted in ColdFusion | Posted on 02-12-2009 | 7,564 views
James Brown (yes, him) asks:
Is there a way to assign a value to a client variable using JavaScript? I know JavaScript runs on the client side and ColdFusion on the server, but with AJAX allowing remote calls to ColdFusion I wonder if it might be possible. Thank you for your help.
Yep, and it's so easy, it's rather trivial. As long as you can make a HTTP request with Ajax, then you can run any code you would imagine.
I've been blogging about jQuery pretty extensively lately so let's do this one just using what ships in ColdFusion 8. First, I'll create a simple form:
2Set Name: <input type="text" name="name" /> <input type="button" value="Set" id="btn" />
3</form>
Next I'll use the cfajaxproxy to bind to the name form field.
This simply says: Generate JavaScript that watches for a change to the name value and pass it to the url: setname.cfm?name={name}, where {name} will be the value of the form field. The tag also allows you to run CFC methods or JavaScript functions. setname.cfm is simply:
2<cfset client.name = url.name>
And that's it. You may want to change when the Ajax call is made. By default it is fired with the onBlur event. So if you click the set button, or anything else on the page, the Ajax call is made. You could modify things a bit and tell it to fire just on the button click:
This bind expression means: Pass name, but don't monitor it (none), and monitor the btn button's click event. Why is there a & between the two binds? I needed a valid URL for the request and the bind has to actually be in the URL itself. Not quite as obvious and simple as the first example, but it gives you a bit more control by running just when the button is clicked.


<cfajaxproxy cfc="components.something" jsclassname="callsomething"/>
this has been my life saver over the last few weeks, I love ColdFusion!
In the case of a shopping cart, I have used a CFC as a Javascript object, but nothing gets written to the cart directly using the object, everything is checked against the a database so price and description have to match also its cleaned from injection etc... but the same goes for anything like cfhttp calls etc.
Thanks.
When i try the script above and click the 'set' button, I get a big popup error saying 'Error parsing JSON response:' followed by the HTML of the page.
Any idea where I'm going wrong?
I will start a topic in the forum about this, so I don't pollute the comments area here.
I'm having trouble working this out.
File temp.cfm
function test1()
{
alert("hello");
}
<form>
set name : <input type="text" name="name" /><input type="button" id="btn" />
</form>
<cfajaxproxy bind="url:test.cfm?name={name@none}&{btn@click}" onSuccess = "test1();">
File test.cfm
<cfset session.temp = "url.name">
But it not working as the variable session is being set url.name and not the value from the input.
Please help as it is very important
How do I bind to a non form object?
Thanks, Jim
This is what I have.
<cfajaxproxy bind="url:ShipDetail.cfm?thisSN={SN}"> <!--- my bind --->
// my grid event
onCellSelect: function(rowid,iCol,cellcontent){
var SN = +cellcontent; // trying to set the bind here
if (iCol == 0) {
ColdFusion.Window.show('Shipments'); // display my cfwindow
}
},
My cfwindow:
<cfwindow name="Shipments"
title="Shipments"
modal="true"
resizable="true"
center="true"
initshow="false"
source="ShipDetail.cfm"> </cfwindow>
ShipDetail.cfm contains the cfc and code to display value based on thisSN, which is what I'm trying to bind.
[Add Comment] [Subscribe to Comments]