Solved

cfset onClick

Posted on 2003-10-22
10
1,504 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:theamzngq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9603473
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 :-)
0
 
LVL 11

Expert Comment

by:hart
ID: 9604377
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
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9604559
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 :-)
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 11

Expert Comment

by:hart
ID: 9604655
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


 

0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 9604830
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 :-)
0
 
LVL 11

Expert Comment

by:hart
ID: 9604896
i am not sure what u r saying :-(
anywayz forget it let theamzngq decide...

hope u have no hard feelings ok :-)

Regards
Hart
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9606589
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.

0
 
LVL 2

Author Comment

by:theamzngq
ID: 9607027
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)
0
 
LVL 6

Expert Comment

by:PE_CF_DEV
ID: 9607650
0
 
LVL 2

Accepted Solution

by:
Crazee earned 100 total points
ID: 9618291
Just to clarify the problem...

Currently you know you can post data between pages via URL and you are asking weather you can post it when clicking a button?

In that case you just have to use a form.
<form action="page.cfm" method="post">
 <input type="text" name="myVar" value="someValue">
 <input type="submit" name="Submit">
</form>

Btw, you will never be able to set a CF variable using javascrip as one works on the server side and the other on client side. For example, you may set the value of an input box using javascript and then post it, so you have a feeling that it is set on the client side.

cheers
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question