Jump to content

Recommended Posts

Hi All

I wonder if someone can help me i don't know much about JavaScript. I'm working on a PHP file, it has two dropdown boxes 'starttime' and 'endtime' I want it so that if someone selected 9am 'starttime' the 'endtime' should change to 10am onwards. so basiclly gap of 1hr. Is this possible with Javascript? many thanks

Zed 

Link to comment
https://forums.phpfreaks.com/topic/169806-solved-two-dropdown-boxes/
Share on other sites

Assuming the select lists go from 12:00am to 11:45pm with 15 minute increments, this will do the job. I only included a few hours worth of times for demonstration purposes. If the increments are NOT in 15 minutes, then you need to change the '4' in the function calculation accordingly (30 minutes would be 2, 10 minutes would be 6, etc).

 

<html>
<head>
<script type="text/javascript">

function updateEnd(beginIdx)
{
    endSelectObj = document.getElementById('end');
    endSelectObj.selectedIndex = (beginIdx+4) % endSelectObj.options.length;
}

</script>
</head>

<body>

Start:
<select name="start" id="start" onchange="updateEnd(this.selectedIndex);">
  <option value="8:00">8:00</option>
  <option value="8:15">8:15</option>
  <option value="8:30">8:30</option>
  <option value="8:45">8:45</option>
  <option value="9:00">9:00</option>
  <option value="9:15">9:15</option>
  <option value="9:30">9:30</option>
  <option value="9:45">9:45</option>
  <option value="10:00">10:00</option>
  <option value="10:15">10:15</option>
  <option value="10:30">10:30</option>
  <option value="10:45">10:45</option>
</select>
<br>
End:
<select name="end" id="end">
  <option value="8:00">8:00</option>
  <option value="8:15">8:15</option>
  <option value="8:30">8:30</option>
  <option value="8:45">8:45</option>
  <option value="9:00">9:00</option>
  <option value="9:15">9:15</option>
  <option value="9:30">9:30</option>
  <option value="9:45">9:45</option>
  <option value="10:00">10:00</option>
  <option value="10:15">10:15</option>
  <option value="10:30">10:30</option>
  <option value="10:45">10:45</option>
</select>

</body>
</html>

Start Option values and end options values are now offset by one hour

 

<?php
function createTimeOptions($start, $end)
{
    $options = '';
    for ($time=$start; $time<=$end; $time+=.25)
    {
        $hours = floor($time);
        $minutes = ($time-$hours) * 60;
        $timeStr = date("g:i a", mktime($hours, $minutes, 0, 1, 1, 2000));
        $options .= "<option value=\"{$timeStr}\">{$timeStr}</option>\n";
    }
    return $options;
}
?>
<html>
<head>
<script type="text/javascript">

function updateEndTime(beginTimeSelectObj)
{
    var beginTimeIdx = beginTimeSelectObj.selectedIndex;
    var endTimeSelectObj = document.getElementById('end');
    endTimeSelectObj.selectedIndex = beginTimeIdx;
    var optionColor;
for (var endTimeIdx=0; endTimeIdx<endTimeSelectObj.options.length; endTimeIdx++)
    {
        optionColor = (endTimeIdx<beginTimeIdx) ? '#cecece' : '';
        endTimeSelectObj.options[endTimeIdx].style.backgroundColor = optionColor;
    }
    return;
}
function validateEndTime(endTimeSelectObj)
{
    var beginTimeSelectObj = document.getElementById('start');
    if (endTimeSelectObj.selectedIndex<beginTimeSelectObj.selectedIndex)
    {
        endTimeSelectObj.selectedIndex = beginTimeSelectObj.selectedIndex;
    }
    return;
}
</script>
</head>

<body>

Start:
<select name="start" id="start" onchange="updateEndTime(this);">
<?php echo createTimeOptions(8, 22);  //8am to 10pm ?>
</select>
<br>
End:
<select name="end" id="end" onchange="validateEndTime(this);">
<?php echo createTimeOptions(9, 23);  //9am to 11pm ?>
</select>

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.