hey all

i have 3 drop down boxes: one for the day, month and then the year. however i'd like to have it so that the person choosing the date can't choose a past date. In other words if they try choose for instance the 14th Oct when it's now the 17th Oct, The value selected should not be anything less than the 17th Oct...

there is a little Javascript for sending a msg if the dates are in the past however i can't get the selected date to remain the present date...

<script>
function isValidDate()
{
	var now = new Date();
	
	//advance one day
	now.setDate( now.getDate() - 1 );

	var Day=document.getElementById('daysOfMonth').value;
	var Month=document.getElementById('monthOfYear').value;
	var Year=document.getElementById('year').value;
	
	var str = Month + " " + Day + " " + Year;
	
	var selectedDate= new Date(str);
		
	if(selectedDate < now)
	{
		alert("You may not select dates in the past . . . only future days!!!");
	}	
}
</script>

 <form action="AvailableRooms.php" method="post" name="CheckAvailability" target="_self" id="CheckAvailability">
    <label for="daysOfMonth"></label>
          <div align="right">
            <select name="daysOfMonth" id="daysOfMonth" onchange="isValidDate()">
              <option><?php if($day == 32){$day = 1; echo $day;}else{ echo $day;} ?></option>   <!--problem-->
              <option>....</option>
              <?php 
				for($i = 1; $i <= $days_in_month; $i++)
				{
					echo "<option value=".$i.">".$i."</option>";
				}
			?>
            </select>
          </div>
        <label for="monthOfYear"></label>
          <div align="center">
            <select name="monthOfYear" id="monthOfYear" onchange="isValidDate()">
              <option selected="selected"><?php echo $current_month; ?></option>
              <option>........</option>
              <option value="January">January</option>
              <option value="February">February</option>
              <option value="March">March</option>
              <option value="April">April</option>
              <option value="May">May</option>
              <option value="June">June</option>
              <option value="July">July</option>
              <option value="August">August</option>
              <option value="September">September</option>
              <option value="October">October</option>
              <option value="November">November</option>
              <option value="December">December</option>
            </select>
          </div>
        <label for="year"></label>
          <select name="year" id="year" onchange="isValidDate()">
            <option selected="selected"><?php echo $year; ?></option>
            <option value="<?php echo $year + 1 ?>"><?php echo $year + 1 ?></option>
            <option value="<?php echo $year + 2 ?>"><?php echo $year + 2 ?></option>
          </select>

can anyone help?????????

Recommended Answers

All 7 Replies

<option><?php if($day == 32){$day = 1; echo $day;}else{ echo $day;} ?></option> <!--problem-->

Where did you initialize $day ? BEFORE the if clause you should have:

$day = isset($_POST['daysOfMonth']) && !empty($_POST['daysOfMonth']) ? $_POST['daysOfMonth'] : 1;
$date = time();

$day = date('d', $date); 
$month = date('m', $date); 
$year = date('Y', $date);

//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year);

$current_month = date('F');

dammit sorry this is the rest of the code...

try:

...
          <div align="right">
<?php
if( isset($_POST['daysOfMonth']) &&!empty($_POST['daysOfMonth']) )
{
	$day = $_POST['daysOfMonth']; 
	$month = $_POST['monthOfYear']; 
	$year = $_POST['year'];
}
else
{
	$date = time();

	$day = date('d', $date); 
	$month = date('m', $date); 
	$year = date('Y', $date);
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year);

$current_month = date('F');
?>		
            <select name="daysOfMonth" id="daysOfMonth" onchange="isValidDate()">
              <option>....</option>
		    <?php 
		    		echo sprintf('<option selected="selected" value="%s">%s</option>',$day,$day); 
				for($i = 1; $i <= $days_in_month; $i++)
				{
					echo "<option value=".$i.">".$i."</option>";
				}
			?>
            </select>
...

Well that's more or less what i have there, however what i'm tryin to do is if a date that is invalid is picked it reverts back to selecting the current date. this way no one can make a reservation for a day that has already past...

I think I understand what you are asking now. Within your if clause you need to determine the current m/d/y and use that to reset the values of your drop downs:

<script>
//these will be updated upon load with the initially selected options
var initialDate={'date':-1,'month':-1,'year':-1};
window.onload=function(){
	initialDate.date=document.getElementById('daysOfMonth').selectedIndex;
	initialDate.month=document.getElementById('monthOfYear').selectedIndex;
	initialDate.year=document.getElementById('year').selectedIndex;
}


function isValidDate()
{
	var now = new Date();
	
	//advance one day
	now.setDate( now.getDate() - 1 );

	var Day=document.getElementById('daysOfMonth').value;
	var Month=document.getElementById('monthOfYear').value;
	var Year=document.getElementById('year').value;
	
	var str = Month + " " + Day + " " + Year;
	
	var selectedDate= new Date(str);
		
	if(selectedDate < now)
	{
		alert("You may not select dates in the past . . . only future days!!!");
		//now here you reset the selects to the initial values
		document.getElementById('daysOfMonth').selectedIndex=initialDate.date;
		document.getElementById('monthOfYear').selectedIndex=initialDate.month;
		document.getElementById('year').selectedIndex=initialDate.year;
	}	
}
</script>

well turned out to be way easier.... just refreshed the page and it restored the current date values...

Once

well turned out to be way easier.... just refreshed the page and it restored the current date values...

When those select fiels are all you have on the form (or they appear at the start of the form) then refreshing works fine. Even easier (IMHO) would be to call the reset() method of the form object. But if you first were to have have a name field followede by your selects, then refreshing would erase what was already entered in the name field.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.