Forum Moderators: open

Message Too Old, No Replies

Date drop down in html form.

Date drop down in html form.

         

djm2

3:13 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



I need to write an html form with a <select> box for date of birth. Is there anyway to do this without writing
<option value="1950">1950</option>,
<option value="1951">1951</option>, etc for all the years of last century and this one? Thanks

BlobFisk

3:22 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi djm2,

The quick answer is no - each year needs to be an <option>.

However, you can use a client-side script (like JavaScript), or even better, a server-side language (JSP, ASP, PHP, CGI/Perl) to write this using a for loop.

Increment the writing of the <options> using a for loop, from 19XX to the current year. I would recomment the server side solution if possible, as it will have more support and be more accessible than a client-side solution.

HTH

[edited by: BlobFisk at 3:23 pm (utc) on Jan. 20, 2004]

korkus2000

3:22 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would hesitate to use JS just because some people don't have it enabled although you can use a loop to add the values. I would prefer to use PHP or ASP. In JS you would do something like this:

<script>
document.write("<select>");
for(i=0;i<100;i++){
var dateVar = 1930 + i;
document.write('<option value=\"' + dateVar + '\">' + dateVar + '</option>');
}
document.write("</select>");
</script>

djm2

3:27 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Thanks very much! The Javascript solution work a treat!

Birdman

3:30 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think one advantage to JS is the quicker download time. A form of that nature will be pretty large.

But, Korkus and blobfisk are absolutely correct...some won't be able to use it.

g1smd

11:41 pm on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you provide alternative content for people with javascript turned off, or at least a warning?