I am trying to add an HTML "select dropdown list" for gender. However I want the default value from the database with php framework. Can I do that?
Asked
Active
Viewed 1,755 times
0
Mukesh Chapagain
- 25,063
- 15
- 119
- 120
ylleigh12
- 151
- 2
- 10
-
Your question is pretty vague in the sense that the context is ambiguous... can you provide some of your code for more insight? – Hybrid May 12 '15 at 05:16
-
did you even search SO before posting? http://stackoverflow.com/questions/8299928/change-html-dropdown-default-value-with-a-mysql-value or http://stackoverflow.com/questions/12293939/select-dropdown-default-value or http://stackoverflow.com/questions/14656107/get-data-from-database-to-select-a-default-value-for-the-drop-down-box plus many more http://stackoverflow.com/questions/10915584/is-it-possible-to-query-a-mysql-database-from-a-field-selected-from-dropdown-men – Sean May 12 '15 at 05:18
-
I hope this post answers your question http://stackoverflow.com/questions/18124911/select-option-default-based-on-database-variable – Partha Bisoi May 12 '15 at 05:19
-
select name="Sex"> – ylleigh12 May 12 '15 at 05:20
-
`` – Sean May 12 '15 at 05:39
2 Answers
0
Pseudo code:
$default_gender = getDefaultGenderValueFromDB;
<select name="genderSelected">
<option value="m"<?php echo ($default_gender == 'm')? selected:'';?>>Male</option>
<option value="f"<?php echo ($default_gender== 'f')? selected:'';?>>Female</option>
</select>
Valid code snippet:
<?php $default_gender = 'm'; ?>
<select name="genderSelected">
<option value="m" <?php echo ($default_gender == 'm')? 'selected="selected"':'';?>>Male</option>
<option value="f" <?php echo ($default_gender == 'f')? 'selected="selected"':'';?>>Female</option>
</select>
foxbeefly
- 510
- 3
- 13
-
I've tried this but it the default value is male or whatever that is typed first. – ylleigh12 May 12 '15 at 06:37
-
OK I posted pseudo code - in other words not code that would work but rather an basic outline of how to code. I have changed my answer to include working, valid code. – foxbeefly May 12 '15 at 06:47
0
Try this:
<td>Sex: </td> <td> <select name="Sex">
<?php
$result = mysqli_query($connection, "Select sex from personaltab where empno='$emplno'");
$rows = mysqli_fetch_assoc($result);
$gender =$rows['sex'];
echo "<option if($gender == 'FEMALE') { selected='selected'} value='FEMALE'>FEMALE</option>";
echo "<option value='MALE' if($gender == 'MALE') { selected='selected' }>MALE</option>";
?>
</select> </td></tr>
dont use if else..
Rahul Saxena
- 465
- 4
- 15