...

View Full Version : Date of Birth Dropdown and Database!



StevenPrice
12-21-2011, 01:04 PM
<script type="text/javascript">

/***********************************************
* Drop Down Date select script- by JavaScriptKit.com
* This notice MUST stay intact for use
* Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
***********************************************/

var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];

function populatedropdown(dayfield, monthfield, yearfield){
var today=new Date()
var dayfield=document.getElementById(dayfield)
var monthfield=document.getElementById(monthfield)
var yearfield=document.getElementById(yearfield)
for (var i=0; i<31; i++)
dayfield.options[i]=new Option(i, i+1)
dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
for (var m=0; m<12; m++)
monthfield.options[m]=new Option(monthtext[m], monthtext[m])
monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
var thisyear=today.getFullYear()
for (var y=0; y<20; y++){
yearfield.options[y]=new Option(thisyear, thisyear)
thisyear+=1
}
yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
}

</script>

This script when the year is selected chooses 2011-2030 but I want like 1900-2011 How do I fix this and then how would I make sure that it isn't blank and then sends this data to one field in my database called "dob" in form "0000-00-00".. thanks..

Philip M
12-21-2011, 01:23 PM
Change this


var thisyear=today.getFullYear()
for (var y=0; y<20; y++){


to



var thisyear=1900; // fixed start year of 1900
var nowyear = today.getFullYear();
var diff = nowyear - thisyear +1; // number of years from 1900
for (var y=0; y<diff; y++){


As "thisyear" is no longer an appropriate name for the variable you may wish to rename it to "startyear" throughout. I have named the current year "nowyear".

To make a string of the full date in the form you requested:-


var year = document.getElementbyId("yeardropdown").value;
var month = document.getElementById(""monthdropdown").value;
var day = document.getElementById("daydropdown").value;
var combineddob = year + "-" + "month" + "-" + day;



It cannot be blank as it takes the values from the pre-populated select lists.

I don't expect you will get many people born in 1900! And as tiny babies or the as-yet-unborn are unlikely to register you may think it a good idea to make the final year 2010 if not earlier.


yearfield.options[0]=new Option(today.getFullYear()-1, today.getFullYear()-1, true, true);

Gritters will be working around the clock for the next few hours. - BBC Scotland News.

StevenPrice
12-21-2011, 01:45 PM
So I can mix that around to have it in form day-month-year

How would I sink this into my database then?

Philip M
12-21-2011, 02:02 PM
So I can mix that around to have it in form day-month-year

How would I sink this into my database then?

That depends on your server-side coding. What database are you using? mySQL?

StevenPrice
12-21-2011, 02:05 PM
Yes I have a MySQL Database named myMembers and the field to be put in is called "dob" and the form is "date" and it is default 00-00-0000

I edited this code so I could have 00-00-0000


var day = document.getElementbyId("daydropdown").value;
var month = document.getElementById(""monthdropdown").value;
var year = document.getElementById("yeardropdown").value;
var combineddob = day + "-" + "month" + "-" + year;

So how would I do this now?

Philip M
12-21-2011, 03:40 PM
Yes I have a MySQL Database named myMembers and the field to be put in is called "dob" and the form is "date" and it is default 00-00-0000

I edited this code so I could have 00-00-0000


var day = document.getElementbyId("daydropdown").value;
var month = document.getElementById(""monthdropdown").value;
var year = document.getElementById("yeardropdown").value;
var combineddob = day + "-" + "month" + "-" + year;

So how would I do this now?

Ask in the PHP forum. Javascript cannot access a database.

StevenPrice
12-21-2011, 03:54 PM
I just asked thanks for all your help though :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum