Solved

Javascript var to coldfusion

Posted on 2006-10-24
7
1,068 Views
Last Modified: 2013-12-24
I have the following within a javascript funtion

 var variable1= customeridvalue;

When I show the "variable1" throught an alert it gives the value that I need.  I want to pass that variable1 value to the following page using a hidden field. I don't know how to dump the value of that variable.  I need to do something like this to work:

<input type="hidden" name="customerid"  value="<cfoutput>#variable1#</cfoutput>">  (this doesn't work, of course)

How do I set the value=variable1

0
Comment
Question by:sweetbuttercup
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:mkishline
ID: 17797664
You don't actually need to use coldfusion to do this, javascript can handle it:

<input type="hidden" name="customerid" <script type="text/javascript">document.write('value="' + variable1 + '"');</script> />
0
 
LVL 20

Expert Comment

by:trailblazzyr55
ID: 17797691
With javascript you can use CF variables, but not the other way around...

so if you want to assign a value from coldfusion to a javascript variable, that'll work, then you can pass that variable to a hidden field to submit to the next page....

if this is your hidden field, you'd want to do something like this...

<cfset customeridvalue = "Whatever">

<script>
Function Something(){
  var variable1= <cfoutput>'#customeridvalue#'</cfoutput>;
  document.getElementById("customerid").value =  variable1;
}
</script>

with a hidden field like so....

<input type="hidden" name="customerid" id="customerid">

then it'll grab that value....

wouldn't it be easier though to just do this...

<cfset customeridvalue = "Whatever">
<cfoutput>
    <input type="hidden" name="customerid" id="customerid" value="#customeridvalue#">
</cfoutput>
0
 
LVL 1

Author Comment

by:sweetbuttercup
ID: 17798101
mkishline: I tried your solution but get the output of document.write('value="' + variable1 + '"'); />  printed on the screen and it doesn't pass the value

trailblazzyr55  I need to use the JS variable. I tried using CFSET to assign the vlue of the JS var to a CF var but doesn't work
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 20

Expert Comment

by:trailblazzyr55
ID: 17798263


this form of assigning a CF value to a Javascript one will work...

<script>
Function Something(){
  var variable1= <cfoutput>'#customeridvalue#'</cfoutput>;
  document.getElementById("customerid").value =  variable1;
}
</script>

you have to call the funtion of course to run it, however if you want to do it when the page loads you can do this...

<script>
function init(){
  var variable1= <cfoutput>'#customeridvalue#'</cfoutput>;
  document.getElementById("customerid").value =  variable1;
}
window.onload = init();
</script>

you don't use the cfset's inside the javascript, but you can output the CF vars in javascript to use their values, however since coldfusion is run on the server and javascript on the client, the cf variables are always populated first, the javascript can just access those values that are outputed from CF. this is why you have to use the cfoutput inside the javascript... So you can't use cfset to assign a javascript value to a cf variable becuase the cf is always processed first...

If you want to use a value from javascript, assign that value to a hidden field, submit the form and assign that form variable then to a cf value. It requires a submit because you have to go back to the server where cf is processed.

make sense?
0
 
LVL 1

Author Comment

by:sweetbuttercup
ID: 17798620
My var is a JS var not a CF var. I just need to capture the value of the JS var and submit it to the second page as a hidden field or any other way.  
0
 
LVL 12

Accepted Solution

by:
mkishline earned 500 total points
ID: 17798740
Should have just gone with my first instinct (or just tested the idea before I sent it along :-P ).

<script type="text/javascript">
  variable1 = customerid;
  function update_customerid(){
    document.getElementById('customerid').value = variable1;
  }
</script>

further down:
<input type="text" name="customerid" id="customerid" value="" />
<script type="text/javascript">update_customerid();</script>
0
 
LVL 1

Author Comment

by:sweetbuttercup
ID: 17799016
mkishline : I think my original question was very confusing. I shouldn't have mixed up  Coldfusion on all this to make your life easier. Thank you so much!!!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IIS7 - Wildcard -  Accepting any subdomain for website binding:   i.e. *.domain.com 7 125
PHP Register global 21 88
System Analysis 5 58
WEB Farm 6 70
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

810 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