Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change CF variable via javascript 2

Status
Not open for further replies.

JamesManke

Programmer
Jul 1, 2004
54
CA
After using javascript to change the value of a field without refreshing the page is it possible to store the changed value information into a CF variable?

Thanks

Heres what I got so far...


<script language=javascript>
function changeColor(newColor) {
document.getElementById("color_change").innerHTML = newColor;

}
</script>
<table>
<tr>
<td name="color_change" id="color_change">
<!---This is the information I want to store in a variable--->
Show Color Name Here</td>
</tr>
<tr>
<td>
<select name="colors" onchange='changeColor(this.value)'>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
</td>
</tr>
</table>

<!---Storing text field 'color_change' to variable--->
<CFSET variables.current_color = ????>
 
after you submit the form cf can do whatever you want with the value. not until then though.

If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside.
 
It is possible to set a coldfusion variable without having to refresh the 'calling' page. However, you can't use that variable in the same page without making another trip to the server.

The method to accomplish this is similiar to the one I described in post on Multithreading in MX,
By using of status codes you can achieve this. For example:

Code:
 <!--- Your calling page --->
 <script language="Javascript">
  function setNewColor( newColor ){
    document.location = 'setColor.cfm?newColor='+ newColor;
  }
 </script>
 <form name="myform" action="someaction.cfm">
 <select name="colorChoice" onChange="setNewColor(this.value);">
     <option value="red"> Red </option>
     <option value="blue"> Blue </option>
     <option value="green"> Green </option>
 </select>
 </form>

Then create a template that will 'set' your new color choice. If you plan on using this value later, either store it in a db or for my example, I saved it in a session variable.
Code:
  <!--- setColor.cfm --->
  <cfparam name="url.newColor" type="string" default="">
  
  <!--- Return a response to the browser, telling it that the server has recieved the request successfully but it doesn't have any content to return --->
  <cfheader statusCode="204" />
  
  <!--- Store this variable in session --->
  <cflock scope="session" type="exclusive" timeout="10">
      <cfset session.myColor = url.newColor>
  </cflock>

On your next page request, the session variable 'myColor' will have the value you selected from the form. If you are interested in learning more about the different http status codes check out this document:

Hypertext Transfer Protocol -- HTTP/1.1

jalpino
 
hey jalpino,
You're awesome.

very knowledgeable with docs to back it up with extra info. keep up the great posts.

If the automobile had followed the same development cycle as the computer, a Rolls-Royce would today cost $100, get a million miles per gallon, and explode once a year, killing everyone inside.
 
Thanks cannedRadio, I appreciate the compliment and will try to make usefull posts when I can.

jalpino

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top