Forum Moderators: open

Message Too Old, No Replies

code modification

a little help please

         

WhosAWhata

10:26 pm on Apr 19, 2004 (gmt 0)

10+ Year Member



would anyone be willing to help me modify the following code so that the month NAME (Jan,Feb,Mar...) gets passed through the form?

<SCRIPT>
var date_arr = new Array;
var days_arr = new Array;
date_arr[0]=new Option("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);
function fill_select(f)
{
document.writeln("<SELECT name=\"months\" onchange=\"update_days(FRM)\">");
for(x=0;x<12;x++)
document.writeln("<OPTION onfocus=\"document.FRM.amonth.value='"+x+"'\" value=\""+date_arr[x].value+"\">"+date_arr[x].text);
document.writeln("</SELECT><SELECT name=\"days\"></SELECT>");
selection=f.months[f.months.selectedIndex].value;
}
function update_days(f)
{
temp=f.days.selectedIndex;
for(x=days_arr.length;x>0;x--)
{
days_arr[x]=null;
f.days.options[x]=null;
}
selection=parseInt(f.months[f.months.selectedIndex].value);
ret_val = 0;
if(f.months[f.months.selectedIndex].value == 28)
{
year=parseInt(f.years.options[f.years.selectedIndex].value);
if (year % 4!= 0 ¦¦ year % 100 == 0 ) ret_val=0;
else
if (year % 400 == 0) ret_val=1;
else
ret_val=1;
}
selection = selection + ret_val;
for(x=1;x < selection+1;x++)
{
days_arr[x-1]=new Option(x);
f.days.options[x-1]=days_arr[x-1];
}
if (temp == -1) f.days.options[0].selected=true;
else
f.days.options[temp].selected=true;
}
function year_install(f)
{
document.writeln("<SELECT name=\"years\" onchange=\"update_days(FRM)\">")
for(x=2004;x<2010;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
document.writeln("</SELECT>");
update_days(f)
}
</script><body>
<FORM name="FRM" action=admin.php method="post">
<h1 align=center>JavaScript date selector</h1>
<br>
<br>
<table align="center">
<tr>
<td>
<SCRIPT>fill_select(document.FRM);year_install(document.FRM)</script></td>
</tr></table>
<input type=submit value=submit>
</FORM>

HELP IS MUCH APPRECIATED!

AWildman

4:35 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



You could add a hidden field whose value is changed to equal the month once a month is chosen. Then the value of the hidden field would be passed to your processing script.

WhosAWhata

9:33 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



thats exactly what my first reaction was, i checked out how to do this (due to the not being ablue to use the option.value)

here is the thread (for anyone interested):
[webmasterworld.com...]

is this the best way?

WhosAWhata

9:38 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



they've basically figured out how to get it to this point:

<script type="text/javascript">
var arrayOne = new Array()
arrayOne[1]="One"
arrayOne[2]="Two"
arrayOne[3]="Three"
arrayOne[4]="Four"
arrayOne[5]="Five"

var arrayTwo = new Array()
arrayTwo[1]="some location"
arrayTwo[2]="some other location"
arrayTwo[3]="some url"
arrayTwo[4]="some uri"
arrayTwo[5]="some address"

function someFunction() {
alert(arrayTwo[document.frm.sel.value])
}
</script>

<form name="frm" onsubmit="someFunction(); return false;">
<input type="text" name="text">
<br>
<select name="sel" onchange="document.frm.text.value=arrayOne[this.value];">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="submit" value="Submit">
</form>

but i'm not quite sure how to apply this to the other script...
any suggestions on how to integrate them?

AWildman

11:57 am on Apr 21, 2004 (gmt 0)

10+ Year Member



Here's what I would do.

<SCRIPT>
var date_arr = new Array;
var days_arr = new Array;
date_arr[0]=new Option("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);
function fill_select(f)
{
document.writeln("<SELECT name=\"months\" onchange=\"update_days(FRM)\">");
for(x=0;x<12;x++)
document.writeln("<OPTION onfocus=\"document.FRM.amonth.value='"+x+"'\" value=\""+date_arr[x].value+"\">"+date_arr[x].text);
document.writeln("</SELECT><SELECT name=\"days\"></SELECT>");
selection=f.months[f.months.selectedIndex].value;
}
function update_days(f)
{
temp=f.days.selectedIndex;
for(x=days_arr.length;x>0;x--)
{
days_arr[x]=null;
f.days.options[x]=null;
}
selection=parseInt(f.months[f.months.selectedIndex].value);

var mo = f.months[f.months.selectedIndex].text;
frm.monthname.value = mo;

ret_val = 0;
if(f.months[f.months.selectedIndex].value == 28)
{
year=parseInt(f.years.options[f.years.selectedIndex].value);
if (year % 4!= 0 ¦¦ year % 100 == 0 ) ret_val=0;
else
if (year % 400 == 0) ret_val=1;
else
ret_val=1;
}
selection = selection + ret_val;
for(x=1;x < selection+1;x++)
{
days_arr[x-1]=new Option(x);
f.days.options[x-1]=days_arr[x-1];
}
if (temp == -1) f.days.options[0].selected=true;
else
f.days.options[temp].selected=true;
}
function year_install(f)
{
document.writeln("<SELECT name=\"years\" onchange=\"update_days(FRM)\">")
for(x=2004;x<2010;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
document.writeln("</SELECT>");
update_days(f)
}
</script><body>
<FORM name="FRM" action=admin.php method="post">
<h1 align=center>JavaScript date selector</h1>
<br>
<br>
<table align="center">
<tr>
<td>
<SCRIPT>fill_select(document.FRM);year_install(document.FRM)</script></td>
</tr></table>
<input type = "hidden" name = "monthname" value = ""><-------NEW<input type=submit value=submit>
</FORM>

AWildman

12:08 pm on Apr 21, 2004 (gmt 0)

10+ Year Member



Getting that code to work cross-browser may be a treat. Here is a piece of code referencing the text of a selected index item that I use. This is proven compatible with everything from i.e. 5x + and nn 4.79+

formname[formelementname].options[formname[formelementname].selectedIndex].text

SO, in your case

frm[monthname].options[frm[monthname].selectedIndex].text

WhosAWhata

9:35 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



wildman,
your code (in my php script) outputs

<html><head><title>EDIT</title>
<style type="text/css">
body { background-color:bbdddd; color:000066; }
a:link { color:dd3366; text-decoration:none; }
a:hover { color:aaaa00; text-decoration:none; }
a:active { color:dd3366; text-decoration:none; }
</style>
<SCRIPT>
var date_arr = new Array;
var days_arr = new Array;
date_arr[0]=new Option("January",31);
date_arr[1]=new Option("February",28);
date_arr[2]=new Option("March",31);
date_arr[3]=new Option("April",30);
date_arr[4]=new Option("May",31);
date_arr[5]=new Option("June",30);
date_arr[6]=new Option("July",31);
date_arr[7]=new Option("August",30);
date_arr[8]=new Option("September",30);
date_arr[9]=new Option("October",31);
date_arr[10]=new Option("November",31);
date_arr[11]=new Option("December",30);
function fill_select(f)
{
document.writeln("<SELECT name=\"months\" onchange=\"update_days(FRM)\">");
for(x=0;x<12;x++)
document.writeln("<OPTION onfocus=\"document.FRM.amonth.value='"+x+"'\" value=\""+date_arr[x].value+"\">"+date_arr[x].text);
document.writeln("</SELECT><SELECT name=\"days\"></SELECT>");
selection=f.months[f.months.selectedIndex].value;
}
function update_days(f)
{
temp=f.days.selectedIndex;
for(x=days_arr.length;x>0;x--)
{
days_arr[x]=null;
f.days.options[x]=null;
}
selection=parseInt(f.months[f.months.selectedIndex].value);

var mo = f.months[f.months.selectedIndex].text;
frm.monthname.value = mo;

ret_val = 0;
if(f.months[f.months.selectedIndex].value == 28)
{
year=parseInt(f.years.options[f.years.selectedIndex].value);
if (year % 4!= 0 ¦¦ year % 100 == 0 ) ret_val=0;
else
if (year % 400 == 0) ret_val=1;
else
ret_val=1;
}
selection = selection + ret_val;
for(x=1;x < selection+1;x++)
{
days_arr[x-1]=new Option(x);
f.days.options[x-1]=days_arr[x-1];
}
if (temp == -1) f.days.options[0].selected=true;
else
f.days.options[temp].selected=true;
}
function year_install(f)
{
document.writeln("<SELECT name=\"years\" onchange=\"update_days(FRM)\">")
for(x=2004;x<2010;x++) document.writeln("<OPTION value=\""+x+"\">"+x);
document.writeln("</SELECT>");
update_days(f)
}
</script><body>
<FORM name="FRM" action=admin.php method="post">
<h1 align=center>JavaScript date selector</h1>
<br>
<br>
<table align="center">
<tr>
<td>
<SCRIPT>fill_select(document.FRM);year_install(document.FRM)</script></td>
</tr></table>
<input type = "hidden" name = "monthname" value = ""><-------NEW<input type=submit value=submit>
</FORM>

this generates an "object expected" error on line 79...why?

AWildman

12:02 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



I tried out your new code and now this line is giving me an error:
temp=f.days.selectedIndex;

I don't see where you created the element days before trying to reference it.

WhosAWhata

10:42 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



the original code was found at the JavascriptSource ( [javascript.internet.com...] )