What role does the 'selected="selected"' attribute play in displaying pre-selected options in PHP dropdown lists?
The 'selected="selected"' attribute is used in HTML dropdown lists to pre-select an option when the page loads. In PHP, you can dynamically set the 'selected' attribute for the option that should be pre-selected based on a certain condition or value. This is commonly used when populating dropdown lists with data from a database and you want to display the previously selected option.
<select name="fruit">
<option value="apple" <?php echo ($selectedFruit == 'apple') ? 'selected="selected"' : ''; ?>>Apple</option>
<option value="banana" <?php echo ($selectedFruit == 'banana') ? 'selected="selected"' : ''; ?>>Banana</option>
<option value="orange" <?php echo ($selectedFruit == 'orange') ? 'selected="selected"' : ''; ?>>Orange</option>
</select>
Related Questions
- How can UTF-8 collation settings be adjusted in PHP to properly store and retrieve kyrillische Zeichen?
- What are the implications of changes in email server policies, such as stricter requirements for sender information in PHP scripts?
- How can PHP be used to ensure that data is displayed in equally sized fields within a table?