Results 1 to 9 of 9

Thread: Dynamic Select Box

  1. #1
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Dynamic Select Box

    Hello All,
    How do i dynamically set a select box to output the year e.g 2003, 2004, 2005 etc. What I want is the user to select their age from 3 separate drop down "select" options on a form (day,month, year). I can had code the day and month since the values would not be that much.

    But to handle the year I was thinking about some javascript to look at todays date converted to the year and lets say give me also 100 previous years.

    I hope I make sense.....I am also very new to web development.

    Dallr

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    it actually isn't explained as well as it could be.

    -magicyte

  3. #3
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Your desire was to have a Javascript code to show the date now and 100 years before, or of your desire.
    Try to have this script:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    var num = 100, // Set how many years from now you want to be seen.
    d=new Date();
    for(var i=((d.getFullYear())-num);i<=d.getFullYear();i++)
    	{
    	var opt=document.getElementById('year');
    	opt.options[opt.options.length]=new Option(i,'year'+i);
    	}
    }
    </script>
    <label for="year">Year: </label>
    <select id="year"></select>
    But always note that JS can be disabled. If you want, here's a PHP counterpart:
    PHP Code:
    <label for="year">Year: </label>
    <select id="year">
    <?php
    $num 
    100// Set how many years from now you want to be seen.
    $y=date('Y'); // Get the current year
    for($i=($y-$num);$i<=$y;$i++)
    echo 
    '<option value="year'.$i.'">'.$i.'</option>';
    ?>
    </select>
    Hope it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  4. The Following User Says Thank You to rangana For This Useful Post:

    dallr (09-03-2008)

  5. #4
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    For some reason I was not getting replies to his post. I thought that the default when you created a post was to automatically get replies.

    Thanks much rangana! this is exactly what I wanted. Also thanks for including the PHP code , since in the long run I would also needs something similar once I start doing the php side of this project.

    Can I ask one more question if you may which is along the same lines.

    Question:I want to have the same dynamic select box but using information from my MYSQL database. I want to store all the names of the countries of the world in a table in my database and dynamically populate the select box. I am assuming I need some type of PHP script?

    Thanks in advance

    Dallr

  6. #5
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    For your first problem, try to go to User CP and enable the checkbox to recieve emails.

    For your problem on db, you might find these links useful:
    http://www.php.net/function.mysql-select-db
    http://www.w3schools.com/PHP/php_mysql_select.asp
    http://www.tizag.com/mysqlTutorial/mysqlselect.php

    Get back whenever you're stumped.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. #6
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks Rangana!
    I must say the response time on this forum is wonderful. Because I am new to web development I have joined a couple forums to feel out which one I should make my home. I am looking at things like response rate, knowledge, friendly atmosphere etc.

    Dynamic Drive is certainty going on top of the list at present. This forum was actually recommended to me by someone.

    I would look at the links you provided Rangana and see if I can make some headway. I would post back once I digested the information in the links.

    Peace out!
    Dallr

  8. #7
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Hello Rangana!!
    Sorry to come back to the post after such a long time but I just hit another road block.

    The dynamic year code you gave me does NOT seem to be passing a selected year to my PHP code so I can get it into the database (mysql).

    There is what I am using.
    1. You code in question which shows all the years in the select element. I am using javascript for now only. Will do the php coding for this later as you recommended.
    2.
    PHP Code:
    $yr mysql_real_escape_string($_POST['year']);
    print(
    $yr);// this returns nothing even though a year is selected. 
    The php code in point two should return the year that was selected not so?

    Lastly how can I get the selected element to default to no date. Currently it defaults to 1908 when the form is opened.?

    Sorry for bombarding you with so much questions. Just trying to wrap my head around so much stuff and learn at the same time.

    Dallr

  9. #8
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    You should not forget to add a name attribute on the select tag:
    Code:
    <select id="year" name="year">
    Adding highlighted should return result.

    If you intend to have no year selected by default, then just add an option tag:
    JS:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    var num = 100, // Set how many years from now you want to be seen.
    d=new Date();
    for(var i=((d.getFullYear())-num);i<=d.getFullYear();i++)
    	{
    	var opt=document.getElementById('year');
    	opt.options[opt.options.length]=new Option(i,'year'+i);
    	}
    }
    </script>
    <label for="year">Year: </label>
    <select id="year" name="year">
    <option>---YEAR---</option>
    </select>
    PHP:
    Code:
    <label for="year">Year: </label> 
    <select id="year" name="year">
    <option>---YEAR---</option>
    <?php 
    $num = 100; // Set how many years from now you want to be seen. 
    $y=date('Y'); // Get the current year 
    for($i=($y-$num);$i<=$y;$i++) 
    echo '<option value="year'.$i.'">'.$i.'</option>'; 
    ?> 
    </select>
    See if it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  10. #9
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    I don't know what i changed but it seems to be getting the year now.

    Thanks

Bookmarks

Posting Permissions

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