Zayn Ali
Zayn Ali

Reputation: 4925

Remembering the select field value

public static function load_months()
{
    $output = "\n <select id='month' name='month'>";
    $output .= "\n <option value='0'>Month</option>";

    for($i = 1 ; $i <= 12; $i++)
    {
        $timestamp = mktime(0, 0, 0, $i, 10, 1980);
        $monthName = date("F", $timestamp);
        $output .= "\n <option value='$i'>$monthName</option>";
    }

    $output .= "\n </select>";
    echo($output);
}

I've made this above code to display select of month names. i want to make this script remember the posted selected option just like my name field below from session array. How to do this?

<input type="text" name="fname" value="<?= $user_data->first_name ?>" placeholder="First Name" required>

Upvotes: 0

Views: 57

Answers (3)

Jonas m
Jonas m

Reputation: 2734

You want to make use of the "selected" attribute of the option element.

public static function load_months($user_data = null)
{
    $output = "\n <select id='month' name='month'>";
    $output .= "\n <option value='0'>Month</option>";

    for($i = 1 ; $i <= 12; $i++)
    {
        $selected = ( !is_null($user_data) && isset($user_data->month) && $user_data->month == $i ? 'selected="selected"' : '' );
        $timestamp = mktime(0, 0, 0, $i, 10, 1980);
        $monthName = date("F", $timestamp);
        $output .= sprintf("\n <option value='%s' %s>%s</option>",$i,$selected,$monthName);
    }

    $output .= "\n </select>";
    echo($output);
}

Yet, if you're concerned about scalability. You could do something like this

public static function load_months($selectedMonth = null)
{

    $output = "\n <select id='month' name='month'>";
    $output .= "\n <option value='0'>Month</option>";

    for($i = 1 ; $i <= 12; $i++)
    {
        $selected = ( !is_null($selectedMonth) && is_numeric($selectedMonth) && $selectedMonth == $i ? 'selected="selected"' : '' );
        $timestamp = mktime(0, 0, 0, $i, 10, 1980);
        $monthName = date("F", $timestamp);
        $output .= sprintf("\n <option value='%s' %s>%s</option>",$i,$selected,$monthName);
    }

    $output .= "\n </select>";
    echo($output);
}

And you'll simply pass the selected month to the method.

Upvotes: 2

Vitthal
Vitthal

Reputation: 327

Like this:

if (isset($user_data->month) && $user_data->month == $i) {
            $output .= "\n <option selected='selected' value='$i'>$monthName</option>";
} else {
            $output .= "\n <option value='$i'>$monthName</option>";
}

Upvotes: 0

sunny
sunny

Reputation: 1166

Pass the selected value as a parameter to the function-

public static function load_months($selected)
{
    $output = "\n <select id='month' name='month'>";
    $output .= "\n <option value='0'>Month</option>";

    for($i = 1 ; $i <= 12; $i++)
    {
        $timestamp = mktime(0, 0, 0, $i, 10, 1980);
        $monthName = date("F", $timestamp);
         if($i==$selected)
      {
        $output .= "\n <option value='$i' selected='selected'>$monthName</option>";
      }
       else
      {
           $output .= "\n <option value='$i'>$monthName</option>";
       }
    }

    $output .= "\n </select>";
    echo($output);
}

Upvotes: 0

Related Questions