View Full Version : Dynamic Select Box
dallr
09-01-2008, 07:48 PM
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
magicyte
09-02-2008, 01:11 AM
it actually isn't explained as well as it could be.
-magicyte
rangana
09-02-2008, 05:26 AM
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:
<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:
<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.
dallr
09-03-2008, 03:40 PM
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
rangana
09-03-2008, 03:48 PM
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.
dallr
09-03-2008, 03:55 PM
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
dallr
09-10-2008, 07:38 PM
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.
$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
rangana
09-11-2008, 12:05 AM
You should not forget to add a name attribute on the select tag:
<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:
<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:
<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.
dallr
09-11-2008, 12:32 AM
I don't know what i changed but it seems to be getting the year now.
Thanks
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.