user3774008
user3774008

Reputation: 45

Control the default selection in select form

I want that when the page loads if $a=x,then option1 appears in the select form else if $a=y, option2 appears in the select form else Select should appear.How can I achieve this?

All the code below is in every row of a table and the number of rows are unknown.

PHP:

 $a;
    echo "<div class='res'> State:";
            if ($a[0]!="") $a=x;
            else if ($b[0]!="") $a=y;
            else echo "None";

echo "<form class='form-horizontal'>
    <fieldset  >   
        <span class='control-group' >
        <span  class='controls'>
            <select id='fl' class='form-control' style='cursor:pointer;'>
                <option selected='selected' style='display:none;' value='0'>Select</option>
                <option value='1'>Option1</option>
               <option value='2'>Option2</option>
               </select>

        </span>
        </span>
        <div><button id='mybtn' type='button'>Save</button></div>
    </fieldset>

</form>";

echo "</div";

Upvotes: 0

Views: 124

Answers (4)

Justin Killen
Justin Killen

Reputation: 737

You don't need "selected='selected'", just simply selected:

<option selected value='0'>Selected</option>

typically, I'd set these with a ternary statement, like:

?>
<option <?=$a=='':'selected'?''?> value='0'>Select</option>
<option <?=$a=='x':'selected'?''?> value='1'>Option 1</option>
<option <?=$a=='y':'selected'?''?> value='2'>Option 2</option>
<?

Upvotes: 0

Barmar
Barmar

Reputation: 782130

switch($a) {
case 'x':
    $default = 1;
    break;
case 'y':
    $default = 2;
    break;
default:
    $default = 0;
}

echo "<form class='form-horizontal'>
    <fieldset  >   
        <span class='control-group' >
        <span  class='controls'>
            <select id='fl' class='form-control' style='cursor:pointer;'>
                <option " . ($default == 0 ? "selected='selected'" : "") . " style='display:none;' value='0'>Select</option>
                <option " . ($default == 1 ? "selected='selected'" : "") . " value='1'>Option1</option>
                <option " . ($default == 2 ? "selected='selected'" : "") . " value='2'>Option2</option>
            </select>

        </span>
    </span>
    <div><button id='mybtn' type='button'>Save</button></div>
    </fieldset>

</form>";

Upvotes: 1

cjc
cjc

Reputation: 751

add the following code before the echo:

$option1 = '';
$option2 = '';
$showDefault = "<option value=''>Select</option>";
if ($a == $x) {
    $option1 = 'selected';
    $showDefault = '';
} else if ($a == $y) {
    $option2 = 'selected';
    $showDefault = '';
}

Then change the lines with the HTML options to be:

"$showDefault
<option value='1' $option1 >Option1</option>
<option value='2' $option2 >Option2</option>"

This will allow you to hide the option "Select" unless it is needed. There are other ways of doing it but this way is pretty straight forward.

Upvotes: 0

Dan
Dan

Reputation: 11104

I would build up an array of options like :

$option = [0=>'select', 1=>'option1', 2=>'option2' ...];

Then, to build your select box; just loop through the array:

With a condition to select the appropriate default.

foreach($option as $k=>$v){
  if( <your condition> ){
    $selected = 'selected';
  }else{
    $selected = "";
  }
  echo "<option value='$k' '$selected'>$v</option>;
}

If your condition is simple you could use a ternary operator to simplify the code. $selected = (condition) ? 'value for true' : 'value for false';

Upvotes: 1

Related Questions