user1881090
user1881090

Reputation: 741

How to state whether option is selected and display correct values in drop down menu

Below I have a drop down menu where it will keep the display of the option selected in the drop down menu if there is a validation error in the form, else if the form has no errors then drop down menu will go back to displaying the "Please Select" option:

  $validSubmission = isset($_POST['registerbtn']) isset($getyear);

    $min_year = 1;
    $max_year = 10;
    $years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
    $yearHTML = '';
    $yearHTML .= '<select name="year" id="yearDrop">'.PHP_EOL; 
    $yearHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
        foreach ($years as $year) {
            if ($validSubmission && $year == $_POST['year'] && $numrows != 1) {
                $yearHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
            } else {
                $yearHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
            }
        }

    $yearHTML .= '</select>';

The values for the above drop down are:

1
2
3
4
5
...

10

But I am having problem using the same method with the drop down below as the drop down contains two types of options, those which contains a single number for a year, and those which displays multiple values for a year with a / in between as below:

1
1/2
2
2/3
3
3/4
4
4/5
5
....
10

My question is how can I use the format above for the drop down menu code below in order to keep the selected option selected if there is an error in the form, else if form has no errors then go back to displaying "Please Select" value in the drop down menu?

 $validSubmission = (isset($_POST['createbtn']) && isset($getcourseid) && isset($getcoursename) && isset($getduration));

$durationHTML = '';
$durationHTML .= '<select name="duration" id="durationDrop">'.PHP_EOL; 
$durationHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  
    if ($year != $max_year) {
         $nextYear = $year + 1;
         $durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;              
    }
}
$durationHTML .= '</select>'; 

Upvotes: 0

Views: 148

Answers (1)

David M&#252;ller
David M&#252;ller

Reputation: 5351

try it like this:

foreach ($years as $year) 
{
    $nextYear = $year + 1;

    //check for the "simple years"
    if ($validSubmission && $year == $_POST['duration'])
    {
        $durationHTML .= "<option selected>$year</option>".PHP_EOL;  
    }
    else
    {
        $durationHTML .= "<option>$year</option>".PHP_EOL; 
    }

    //check for the timespans
    if ($year != $max_year) 
    {
        $nextYear = $year + 1;

        if ($validSubmission && ($year . "/" . $nextYear) == $_POST['duration'])
        {
            $durationHTML .= "<option selected>$year/$nextYear</option>".PHP_EOL;    
        }
        else
        {
            $durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;  
        }
    }
}

Even though it is not tested, it should get you started. Hope it helps!

Upvotes: 1

Related Questions