Solved

cfset onClick

Posted on 2003-10-22
10
1,448 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
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 14

Expert Comment

by:Renante Entera
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Expert Comment

by:hart
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 11

Expert Comment

by:hart
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 2

Accepted Solution

by:
Crazee earned 100 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now