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!

Dynamically populate text box

Status
Not open for further replies.

calista

Programmer
Jan 24, 2001
545
US
I am working on a time sheet form. I have the day of the week(e.g. Tuesday) in a dropdown menu and a text box for the date(Jul 24, 2001). What I would like to do, if possible, is, when the user selects the day of the week, the date is automatically filled in. I calculate all the dates based on the week-ending date selected on a previous page. Here is the code for the form:
Code:
ORM ACTION="ProcessTime.cfm" METHOD="post" NAME="TimeSheet" ID="TimeSheet">
	<TABLE ALIGN=&quot;center&quot; CELLSPACING=&quot;2&quot; CELLPADDING=&quot;2&quot; BORDER=&quot;1&quot;>
	<TR>
	    <TD><SELECT NAME=&quot;DayOfWeek&quot; SIZE=&quot;5&quot;>
				<OPTION VALUE=&quot;Monday&quot; ONCLICK=<CFSET DateToday=#MonDate#>>Monday</OPTION>
				<OPTION VALUE=&quot;Tuesday&quot; ONCLICK=<CFSET DateToday=#TuesDate#>>Tuesday</OPTION>
				<OPTION VALUE=&quot;Wednesday&quot; ONCLICK=<CFSET DateToday=#WedDate#>>Wednesday</OPTION>
				<OPTION VALUE=&quot;Thursday&quot; ONCLICK=<CFSET DateToday=#ThursDate#>>Thursday</OPTION>
				<OPTION VALUE=&quot;Friday&quot; ONCLICK=<CFSET DateToday=#FriDate#>>Friday</OPTION>
				<OPTION VALUE=&quot;Saturday&quot; ONCLICK=<CFSET DateToday=#SatDate#>>Saturday</OPTION>
				<OPTION VALUE=&quot;Sunday&quot; ONCLICK=<CFSET DateToday=#SunDate#>>Sunday</OPTION>
			</SELECT>
		</TD>
	    <TD></TD>
	    <TD><INPUT TYPE=&quot;text&quot; NAME=&quot;NameCode&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;20&quot; VALUE=&quot;#DateToday#&quot;></TD>
</TR>
</TABLE>
Any ideas, or is this just not practical? Calista :-X
Jedi Knight,
Champion of the Force
 
Something like this?

<script language=&quot;javascript&quot;>
function fill(day)
{ document.forms[0].NameCode.value = day.value;
}
</script>

<cfoutput>
<form>
<select name=&quot;dayofweek&quot; onchange=&quot;fill(this)&quot;>
<option value=&quot;&quot;>select a day</option>
<cfloop from=&quot;1&quot; to=&quot;7&quot; index=&quot;day&quot;>
<option value=&quot;#dayofweekasString(day)#&quot;>#dayofweekasString(day)#</option>
</cfloop>
</select>

<INPUT TYPE=&quot;text&quot; NAME=&quot;NameCode&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;20&quot; VALUE=&quot;&quot;>
</form>
</cfoutput>
 
Try this:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
function putDate(form,day){
form.NameCode.value = day;
}
//-->
</SCRIPT>

[COLOR=666666]<!--- Date variable passed from the previous page Can be any day of the week --->[/color]
<CFPARAM NAME=&quot;FORM.weekending&quot; DEFAULT=&quot;7/28/01&quot; TYPE=&quot;date&quot;>

[COLOR=666666]<!--- Take the date variable and calculate the Sunday date of that week --->[/color]
<CFSET vSubtract = - (DayOfWeek(FORM.weekending)-1)>
<CFSET vStartDate = DateAdd(&quot;d&quot;,vSubtract,FORM.weekending)>

<CFOUTPUT>
[COLOR=000080][COLOR=FF8000]<SELECT NAME=&quot;DayOfWeek&quot; SIZE=&quot;5&quot; ONCHANGE=&quot;putDate(this.form,this.value)&quot;>[/color][/color]
<CFLOOP FROM=&quot;1&quot; TO=&quot;7&quot; INDEX=&quot;i&quot;>
[COLOR=000080][COLOR=FF8000]<OPTION VALUE=&quot;#DateFormat(vStartDate,&quot;ddd, mmm d, yyyy&quot;)#&quot;>[/color][/color]
#DayOfWeekAsString(i)#
[COLOR=000080][COLOR=FF8000]</OPTION>[/color][/color]
[COLOR=666666]<!--- Increment Date --->[/color]
<CFSET vStartDate = DateAdd(&quot;d&quot;,1,vStartDate)>
</CFLOOP>
[COLOR=000080][COLOR=FF8000]</SELECT>[/color][/color]
</CFOUTPUT> - tleish
 
Boy! Talk about tunnel vision. I was so intent on making my online form look exactly like the paper form, I completely overlooked a much simpler solution. The DateFormat function can give you the day of the week in addition to the rest of it. So, this is my solution.
Code:
<SELECT NAME=&quot;TodayIs&quot;>
				<CFLOOP INDEX=&quot;DateIdx&quot; FROM=&quot;-6&quot; TO=&quot;0&quot; STEP=&quot;1&quot;>
					<CFSET TodayIs = #DateAdd(&quot;d&quot;,DateIdx,WeekEnding)#>
					<OPTION VALUE=&quot;#TodayIs#&quot;<CFIF #DateIdx# EQ -5>SELECTED</CFIF>>#DateFormat(TodayIs,&quot;ddd mmm dd, yyyy&quot;)#</OPTION>
				</CFLOOP>
			</SELECT>
Now, it's all in one field, which is fine. Thanks for the script, guys, I'm sure they'll come in handy somewhere around here. I'm very much a newbie at JavaScript, so I cut and paste or adapt as much as possible. Thanks for the loop idea, too. Don't know why I didn't think that myself. I've even been trying to make my code more efficient, of late, by using loops. Thanks, again! Calista :-X
Jedi Knight,
Champion of the Force
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top