Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Question Date of Birth Dropdown and Database!

    Code:
    <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..

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Change this

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

    Code:
    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:-

    Code:
    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.

    Code:
    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.
    Last edited by Philip M; 12-21-2011 at 01:39 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    So I can mix that around to have it in form day-month-year

    How would I sink this into my database then?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by StevenPrice View Post
    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?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    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

    Code:
    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?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by StevenPrice View Post
    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

    Code:
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Dec 2011
    Posts
    32
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I just asked thanks for all your help though


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •