Solved

cfset onClick

Posted on 2003-10-22
10
1,485 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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