Solved

cfset onClick

Posted on 2003-10-22
10
1,474 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Redirect website ! 4 54
Asp.net plan security, reliable, stable 2 120
How to best troubleshoot slow internet connections via proxy server? 2 81
WebSite Direction 1 41
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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