Kash
Kash

Reputation: 339

Set a default value to a Combobox in php

I modify a code which gives a set of values as dropdown in a combobox. But I want to set a default value to the combobox while the drop down is there. This is my code. If you can help me I really appreciate your help. Thnks in advance.

//Add a drop down list of countries

<div class="col-sm-offset-2 col-xs-9">
  <select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
    <option value=""> Select </option>
    <?php foreach($this->countries as $pro){
      echo '<option value="'.$pro->id.'">'.$pro->country_name.'</option>'; 
    }
?>
  </select>
</div>

Upvotes: 2

Views: 100

Answers (1)

Dhruv
Dhruv

Reputation: 2100

Add this Condition

<div class="col-sm-offset-2 col-xs-9">
  <select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
    <option value=""> Select </option>
    <?php foreach($this->countries as $pro){
      echo '<option value="'.$pro->id.'" '.if($pro->country_name == "india"){.'selected'.}.'>'.$pro->country_name.'</option>'; 
    }
?>
  </select>
</div>

Upvotes: 1

Related Questions