Link to home
Start Free TrialLog in
Avatar of David Williamson
David WilliamsonFlag for United States of America

asked on

cfset onClick

Is there a way, using onClick, to set a CF variable?  In other words, is there a way to set a variable upon a link/button being clicked, besides passing it in the URL?
Avatar of Renante Entera
Renante Entera
Flag of Philippines image

You can have your code this way :

<script language="JavaScript">
function set_cfvar()
{
  <cfset cfvar = 'This is the new value of your cf variable ...'>
  alert('<cfoutput>#cfvar#</cfoutput>');
  <!--- Then proceed with other processing here --->
}
</script>

<a href="javascript:set_cfvar()">Click this Link</a><br><br>
<input type="button" value="Click this Button" onClick="set_cfvar()">

Goodluck !
eNTRANCE2002 :-)
javascript is client side where as cf is server side
u can never reset a variable in a javascript function....

what enterance 2002 has written would be same as this

top of the page
 <cfset cfvar = 'This is the new value of your cf variable ...'>

<script language="JavaScript">
function set_cfvar()
{
 alert('<cfoutput>#cfvar#</cfoutput>');
 <!--- Then proceed with other processing here --->
}
</script>

<a href="javascript:set_cfvar()">Click this Link</a><br><br>
<input type="button" value="Click this Button" onClick="set_cfvar()">


u can never reset a cf variable using javascript.
the better way would be to keep a hidden field

that is

say
<cfset variablename = "something">

<script language="JavaScript">
function set_cfvar()
{
 document.frm.variablename.value = new value that u want to set;
 document.frm.submit();
 <!--- Then proceed with other processing here --->
}
</script>
<form name="frm">
<input type="hidden" name="variablename" value="</cfoutput>#variablename#</cfoutput>" >
<a href="javascript:set_cfvar()">Click this Link</a><br><br>
<input type="button" value="Click this Button" onClick="set_cfvar()">
</form>

Regards
Hart
Actually hart, before I posted my comments. I already test the code to run it in my computer. Then it works perfectly.

I can set my cf variable inside the javascript function. Haven't you tried it ???

Regards !
eNTRANCE2002 :-)
i am not telling that ur alert won't work.

i am just saying this that variable is not getting set inside the function.
its already defined when the page is called.

check this codeout

<script language="JavaScript">
function set_cfvar()
{
<cfset cfvar = 'This is the new value of your cf variable ...'>
 alert('<cfoutput>#cfvar#</cfoutput>');
 <!--- Then proceed with other processing here --->
}
alert('<cfoutput>#cfvar#</cfoutput>')
</script>


now if i tell u that the alert written outside the function will get executed.
and u will also get the value of cfvar in the alert.

so is that variable getting defined inside the function or what?

see i don't want to question ur code i am just saying that cf variables are never formed in javascipt conditions or functions.

tell me

if u write inside javascript

if (something != '')
{
<cfset cfvar = "hi">
}
else
{
<cfset cfvar = "hi2">
}

ur alert will still get excuted..
the value will always be hi2 no matter what because the <cfset cfvar = "hi2"> is the final part that gets parsed.

hope u r getting what i am trying to convey..
the variable doesn't get set inside the fiunction, its always present

Regards
Hart


 

Actually, I am just putting a code there alert just to testify that the value of the cf variable "cfvar" has been set to a specified value.

Sorry for having no clarifications about that hart :-(

Perhaps, he must remove that line and then proceed with his specific processes after setting its cf variables to be pass as a parameter for the next page.

Hope that theamzngq solves now the problem.


Best wishes !
eNTRANCE2002 :-)
i am not sure what u r saying :-(
anywayz forget it let theamzngq decide...

hope u have no hard feelings ok :-)

Regards
Hart
Avatar of PE_CF_DEV
PE_CF_DEV

I will agree with hart as he said javascript is client side and anything done is javascript will be process after ColdFusion (which is server side) has finished processing the request. If you do not want the variable in the URL then use hart's answer this will pass the variable in a form and that vairable can be accessed via the form scope upon it being submited to the server. If you want to make the page appear as though it was not submited, asuming that page itself does not need to be refreshed, you can make a frame that is really small on that page that will submit the hidden form. By doing this the Main page will not be refreshed but any new content that the vairable generates will not be shown in the main frame. If you give an example of what it is you are exactly trying to do we may be able to give you a better solution.

Avatar of David Williamson

ASKER

I am fairly new to javascript, so I'll have to experiment in order to judge what works or not...

Specifically,  I want to set a cookie when the user clicks a specific navigation link.

I will give it a try and let you know (trying to meet a deadline)
ASKER CERTIFIED SOLUTION
Avatar of Crazee
Crazee

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial