1

On my form i am giving user access to modify his details, I am fetching details from db using application id and showing it in my form inputs with its value attribute, so user can see and modify his details if he wants to, but how can we set value in select fields that fetched from database.

here suppose variable is $country fetched from db.

$country = 'India';

And the select fields is

<select name="usercountry" id="usercountry">
  <option value="England">England</option>
  <option value="South Africa">South Africa</option>
  <option value="USA">USA</option>
  <option value="India">India</option>
</select>

Here I want this select box to be set to $country value automatically.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74
imgrv
  • 99
  • 3
  • 17
  • The attribute you would conditionally add to the ` – David Jan 25 '18 at 10:40
  • @SuperDJ : Thank you for the solution mate. – imgrv Jan 25 '18 at 11:04
  • @imgrv Sure, no problem. I would like to advise you to watch some tutorials on YouTube. For example from codecourse, he makes really good tutorials which are very explanatory, brief but very easy to follow. – SuperDJ Jan 25 '18 at 11:11

3 Answers3

4

You can do it using Ternary Operator

 <option <?php echo ($country == 'India')?'selected="selected"':'' ?>>

After @Scriptman Comment and suggestion I would like make it more clean

<option <?php echo ($country == 'India')?'selected':'' ?>>
Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
3

You should check the value of the $country variable to the option value.

<select name="usercountry" id="usercountry">
 <option value="England" <?php if($country == 'England'){ echo "selected";}?>>England</option>
 <option value="South Africa" <?php if($country == 'South Africa'){ echo "selected";}?>>South Africa</option>
 <option value="USA" <?php if($country == 'USA'){ echo "selected";}?>>USA</option>
 <option value="India" <?php if($country == 'India'){ echo "selected";}?>>India</option>
</select>
Bachcha Singh
  • 3,850
  • 3
  • 24
  • 38
Sanjit Bhardwaj
  • 893
  • 7
  • 13
1

This example works form 1 to n number of countries

$userCountry = 'India';

//you get it from DB or wherever
$countries = ['India','USA'];

<select name="usercountry" id="usercountry">
  <?php
        foreach($countries as $country){

         echo '<option value="'.$country.'" '.($userCountry == $country ? 'selected' : '').'>'.$country.'</option>';

        }
 ?>
</select>
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
  • thanks ! this is perfectly one what i need, i have more than 15 fields, where users have to select country ! this will reduce the work. – imgrv Jan 25 '18 at 11:02
  • @SuperDJ : what if i want placeholder here like choose country with NA Value too.. – imgrv Jan 25 '18 at 12:05
  • @imgrv Do you mean a placeholder like ` – SuperDJ Jan 25 '18 at 12:11
  • @SuperDJ : No thatss not the exact point, i mean if in my form there is a select field that is not applicable to user and he doesnt want to select it then by default what data will go to db, it will be blank or with some value like first option in select dropdown using the above code. – imgrv Jan 25 '18 at 12:20
  • @imgrv if not data is selected than it should be blank. However if you use the above select and there is a option selected than this value will return to the db eventough the user didn't alter the select – SuperDJ Jan 25 '18 at 12:39