Dynamic date drop down select menu in php

<script>
function validate()
{
if (document.getElementById('subscription_day').value == "")
{
alert( "<?php echo "Please Select Day"; ?>" );
document.adminForm.subscription_day.focus();
return false;
}
else if (document.getElementById('subscription_month').value == "")
{
alert( "<?php echo "Please Select Month"; ?>" );
document.adminForm.subscription_month.focus();
return false;
}
else if (document.getElementById('subscription_year').value == "" )
{
alert( "<?php echo "Please Select Year"; ?>" );
document.adminForm.subscription_year.focus();
return false;
}
}
function setDOB()
{
var day1 = document.getElementById('subscription_day');
var day2 = day1.options[day1.selectedIndex].text;
if(day2.length < 2 ) day2 = '0'+day2;
var month1 = document.getElementById('subscription_month');
var month2 = month1.options[month1.selectedIndex].value;
if(month2.length < 2 ) month2 = '0'+month2;
//alert('Montyh is :'+month2);
var year1 = document.getElementById('subscription_year');
var year2 = year1.options[year1.selectedIndex].text;
var strText= day2 +'-'+ month2 + '-'+ year2;
document.getElementById('dob').value= strText;
}
</script>
<form action="" method="post" name="adminForm" id="adminForm"
onsubmit="return validate(); ">
<table>
<tr>
<td>Date of Birth:-</td>
<td>
<select name="day" id="subscription_day" style="width:60px;" onchange="setDOB()">
<option value="">Day</option>
<?php
$strDay='';
for ($i=1; $i<=31; $i++) {
$strDay=(string)$i;
if($i<10) $strDay='0'.$strDay;
?>
<option value="<?php echo $i; ?>">
<?php echo $strDay; ?>
</option>
<?php
}
?>
</select>

<select name="month" id="subscription_month" style="width:60px;" onchange="setDOB()">
<option value="">Month</option>
<?php
function getMonthList()
{
return array( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec");
}
$list=getMonthList();
$index=1;
foreach ( $list as &$row_data ) {
?>
<option value="<?php echo $index; ?>">
<?php echo $row_data; ?>
</option>
<?php
$index=$index+1;
}
?>
</select>

<select name="year" id="subscription_year" style="width:60px;" onchange="setDOB()">
<option value="">Year</option>
<?php
for ($i=1950; $i<date('Y'); $i++) {
?>
<option value="<?php echo $i; ?>">
<?php echo $i; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
</table>
</br>
<input type="hidden" id="dob" name="dob" value="" />
<input type="submit" value="Save" />
<?php
if(isset($_POST['dob']))
{
$a=$_POST['dob'];
echo "Date of Birth is:-".$a;
}
?>
</form>

Output

Vineet Kumar Saini

Dynamic date in php














If we click on save button without select year then get an error.

Vineet Kumar Saini

Dynamic date in php

Vineet Kumar Saini

Advertisements
Categories: Joomla, PHP & Joomla | Tags: , , , , , , , , | 7 Comments

Post navigation

7 thoughts on “Dynamic date drop down select menu in php

  1. It’s always good to learn how to do things without libraries – but now, check this out:

    jQuery date picker: http://jqueryui.com/datepicker/
    jQuery Mobile date picker: http://jquerymobile.com/demos/1.0a4.1/experiments/ui-datepicker/

    Both solutions are good, hand-writing and using 3rd party. By hand-writing your own, you get more control with less work. Still, it’s nice to let others sweat the fine details, like staying current and collecting bug reports for little widgets 😉

  2. Sanjay

    $strDay=(string)$i;
    if($i<10) $strDay='0'.$strDay;—why we use this

  3. Sanjay

    $index=1;

    also ..why these are used..plz reply soonn

  4. It is Failed to check Leap Year

    • Hi Praveen,
      I know very well ‘It is Failed to check Leap Year’ but you can use javascript validation for leap year. If you can’t this , please tell me .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: