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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a JavaScript variable back to CF

Status
Not open for further replies.

calista

Programmer
Joined
Jan 24, 2001
Messages
545
Location
US
How do I do that? My test code is shown below. What I am trying to accomplish is this: When the user selects a Starting Time, I want the drop-down menu to automatically be selected for 1 hour later. I can get the Starting Time passed to the JavaScript, and I can certainly add 1 to it. But, how do I get that value passed back to my form? As you can see, I tried a hidden field, but that didn't work. Alternatively, can CFSCRIPT be used with an event like "OnChange"? This can't be that hard, but I don't get it. Any suggestions greatly appreciated! Thanks!
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<HTML>
<HEAD>
	<TITLE>My Test</TITLE>
	<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
	<!--
	function UpdateTimes(which)
		{
			var TempVar = parseInt(document.MyForm.StartDate_Hour.value);
			NewTime = TempVar + 1;
			document.MyForm.HoldTime.value = NewTime;
		}
	//-->
	</SCRIPT>
</HEAD>

<BODY>
<FORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; METHOD=&quot;post&quot; NAME=&quot;MyForm&quot;>
<INPUT TYPE=&quot;hidden&quot; NAME=&quot;HoldTime&quot; VALUE=&quot;0&quot;>
<TABLE>
<TR><TD> </TD></TR>
<!--- The start date/time of the reservation --->
<TR>
	<TD><B>Start Time</B></TD>
	<TD>
		<SELECT NAME=&quot;StartDate_Hour&quot; ONCHANGE=&quot;UpdateTimes(this)&quot;>
		<OPTION>Choose Starting Time.</OPTION>
		<!--- Display business hours --->
		<CFLOOP INDEX=&quot;X&quot; FROM=&quot;8&quot; TO=&quot;18&quot;>
			<CFSET PROPER_HOUR=X>
			<CFSET AMPM=&quot;AM&quot;>
			<CFIF PROPER_HOUR GTE 12>
				<CFIF PROPER_HOUR GT 12>
					<CFSET PROPER_HOUR=PROPER_HOUR - 12>
				</CFIF>
				<CFSET AMPM=&quot;PM&quot;>
			</CFIF>
			<CFOUTPUT>
			<OPTION VALUE=&quot;#X#:00&quot;>#PROPER_HOUR#:00 #AMPM#
			<CFIF X LT 18>
				<OPTION VALUE=&quot;#X#:30&quot;>#PROPER_HOUR#:30 #AMPM#
			</CFIF>
			</CFOUTPUT>
		</CFLOOP>
		</SELECT>
	</TD>
</TR>
<!--- This inserts a blank line for formating purposes. --->
<TR><TD> </TD></TR>
<CFIF IsDefined(&quot;Form.HoldTime&quot;)>
	<FONT COLOR=&quot;Blue&quot; SIZE=&quot;+2&quot;>HoldTime is defined and is equal to #Form.HoldTime#.</FONT>
<CFELSE>
	<FONT COLOR=&quot;Red&quot; SIZE=&quot;+2&quot;>HoldTime is NOT defined.</FONT>
</CFIF>
<TR><TD> </TD></TR>
<!--- The end date/time of the reservation --->
<TR>
	<TD><B>End Time:</B></TD>
	<TD>
		<SELECT NAME=&quot;EndDate_Hour&quot;>
		<!--- Display business hours --->
		<CFLOOP INDEX=&quot;X&quot; FROM=&quot;8&quot; TO=&quot;18&quot;>
			<CFSET PROPER_HOUR=X>
			<CFSET AMPM=&quot;AM&quot;>
			<CFIF PROPER_HOUR GTE 12>
				<CFIF PROPER_HOUR GT 12>
					<CFSET PROPER_HOUR=PROPER_HOUR - 12>
				</CFIF>
				<CFSET AMPM=&quot;PM&quot;>
			</CFIF>
			<CFOUTPUT>
			<OPTION VALUE=&quot;#X#:00&quot;>#PROPER_HOUR#:00 #AMPM#
			<CFIF X LT 18>
				<OPTION VALUE=&quot;#X#:30&quot;>#PROPER_HOUR#:30 #AMPM#
			</CFIF>
			</CFOUTPUT>
		</CFLOOP>
		</SELECT>
	</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
Calista :-X
Jedi Knight,
Champion of the Force
 
&quot;But, how do I get that value passed back to my form?&quot; --> if you want CF to see it you have to send it to the server - remember again and again that : javascript is client side whereas coldfusion is server side
so if you want to use your javascrit function change it to
.... document.MyForm.HoldTime.value = NewTime;
document.MyForm.submit()

// or alternatively document.location=<here past the url for the form>?HoldTime=NewTime

and then in your select box, just
<option value=whatever <cfif isdefined &quot;HoldTime&quot;><cfif HoldTime eq ProperHour>selected</cfif></cfif>>#ProperHour#</option>

// or url.HoldTime, not sure as i don't have cf here anymore

play with it and let me know
 
OK, I've changed the script to the following:
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
	<!--
	function UpdateTimes(which)
		{
			var TempVar = parseInt(document.MyForm.StartDate_Hour.value);
			NewTime = TempVar + 1;
			document.location.href=&quot;MyTest.cfm?HoldTime=NewTime&quot;;
		}
	//-->
	</SCRIPT>
What happens is this: It reloads the form, but loses the start time that was selected, and it passes the string &quot;NewTime&quot; as the value for &quot;HoldTime&quot;. I've tried various means of getting it to pass the value of &quot;NewTime&quot; without success. I tried ##, and () around it. I didn't try the document.MyForm.submit() option, because this is only a small part of a larger form, so I don't want to submit it at this point. I'm just taking this portion of it and working on it in isolation to write the script. Thanks, iza! Calista :-X
Jedi Knight,
Champion of the Force
 
It reloads the form,
it actually SENDS the form to the server
but loses the start time that was selected,
of course !!! if you don' rewrite it it's lost !!
and it passes the string &quot;NewTime&quot; as the value for &quot;HoldTime&quot;.
yes, read what you wrote : HoldTime=NewTime !!!
I've tried various means of getting it to pass the value of &quot;NewTime&quot;without success. I tried ##, and () around it.
and how about : document.location.href=&quot;MyTest.cfm?HoldTime=&quot;+NewTime; ?!!!!
this is a JAVASCRIPT problem - you're in a JAVASCRIPT function - and in almost ANY laguage anyway : if something is in quotes &quot;&quot; it won't get evaluated !!!
and what's worst, using ## meant you expected COLDFUSION to know its value whereas it has NOT been submitted to the server !!!
i don't know how i could say that an other way : javascript is client side whereas coldfusion is server side
 
is it working now ? how did you fix it ?
 
Yes, iza, I did. I started from scratch and this is what I came up with. Well, actually, I studied another script line by line that did something similar and managed to come up with the following. I'm sure it's by no means the best or most elegant solution, but it works.
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>

<html>
<head>
	<title>My Chooser</title>
	<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
	<!--
	var EndTimeArray = new Array();
		EndTimeArray[0]  = &quot;('Select Ending Time')&quot;;
		EndTimeArray[1]  = &quot;('08:30 AM')&quot;;
		EndTimeArray[2]  = &quot;('09:00 AM')&quot;;
		EndTimeArray[3]  = &quot;('09:30 AM')&quot;;
		EndTimeArray[4]  = &quot;('10:00 AM')&quot;;
		EndTimeArray[5]  = &quot;('10:30 AM')&quot;;
		EndTimeArray[6]  = &quot;('11:00 AM')&quot;;
		EndTimeArray[7]  = &quot;('11:30 AM')&quot;;
		EndTimeArray[8]  = &quot;('12:00 PM')&quot;;
		EndTimeArray[9]  = &quot;('12:30 PM')&quot;;
		EndTimeArray[10] = &quot;('01:00 PM')&quot;;
		EndTimeArray[11] = &quot;('01:30 PM')&quot;;
		EndTimeArray[12] = &quot;('02:00 PM')&quot;;
		EndTimeArray[13] = &quot;('02:30 PM')&quot;;
		EndTimeArray[14] = &quot;('03:00 PM')&quot;;
		EndTimeArray[15] = &quot;('03:30 PM')&quot;;
		EndTimeArray[16] = &quot;('04:00 PM')&quot;;
		EndTimeArray[17] = &quot;('04:30 PM')&quot;;
		EndTimeArray[18] = &quot;('05:00 PM')&quot;;
		EndTimeArray[19] = &quot;('05:30 PM')&quot;;
	
	function PopulateEnd(ThisForm,selected)
	{
		while (EndTimeArray.length < ThisForm.EndDate_Hour.options.length)
		{
			ThisForm.EndDate_Hour.options[(ThisForm.EndDate_Hour.options.length - 1)] = null;
		}
		for (var i=0; i < EndTimeArray.length; i++)
		{
			eval(&quot;ThisForm.EndDate_Hour.options[i]=&quot; + &quot;new Option&quot; + EndTimeArray[i]);
			if (i == selected)
			{
				ThisForm.EndDate_Hour.options[i].selected = true;
			}
		}
	}
	//-->
	</SCRIPT>
</head>

<body>
<FORM ACTION=&quot;#CGI.SCRIPT_NAME#&quot; NAME=&quot;MyForm&quot; ID=&quot;MyForm&quot;>
	<SELECT NAME=&quot;StartDate_Hour&quot; ONCHANGE=&quot;PopulateEnd(document.MyForm,document.MyForm.StartDate_Hour.options[document.MyForm.StartDate_Hour.selectedIndex].value)&quot;>
		<OPTION VALUE=&quot;0&quot;>Select Starting Time</OPTION>
		<OPTION VALUE=&quot;1&quot;>08:00 AM</OPTION>
		<OPTION VALUE=&quot;2&quot;>08:30 AM</OPTION>
		<OPTION VALUE=&quot;3&quot;>09:00 AM</OPTION>
		<OPTION VALUE=&quot;4&quot;>09:30 AM</OPTION>
		<OPTION VALUE=&quot;5&quot;>10:00 AM</OPTION>
		<OPTION VALUE=&quot;6&quot;>10:30 AM</OPTION>
		<OPTION VALUE=&quot;7&quot;>11:00 AM</OPTION>
		<OPTION VALUE=&quot;8&quot;>11:30 AM</OPTION>
		<OPTION VALUE=&quot;9&quot;>12:00 PM</OPTION>
		<OPTION VALUE=&quot;10&quot;>12:30 PM</OPTION>
		<OPTION VALUE=&quot;11&quot;>01:00 PM</OPTION>
		<OPTION VALUE=&quot;12&quot;>01:30 PM</OPTION>
		<OPTION VALUE=&quot;13&quot;>02:00 PM</OPTION>
		<OPTION VALUE=&quot;14&quot;>02:30 PM</OPTION>
		<OPTION VALUE=&quot;15&quot;>03:00 PM</OPTION>
		<OPTION VALUE=&quot;16&quot;>03:30 PM</OPTION>
		<OPTION VALUE=&quot;17&quot;>04:00 PM</OPTION>
		<OPTION VALUE=&quot;18&quot;>04:30 PM</OPTION>
		<OPTION VALUE=&quot;19&quot;>05:00 PM</OPTION>
	</SELECT>
	
	<SELECT NAME=&quot;EndDate_Hour&quot;>
		<OPTION>Select Ending Time</OPTION>
	</SELECT>
</FORM>
</body>
</html>
Thanks for your help! Calista :-X
Jedi Knight,
Champion of the Force
 
yes it's even wiser as you keep all your manipulations client side : it will be faster !!! great :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top