How can predefined values be set in a <select> element in PHP forms?
When working with PHP forms, you can set predefined values in a <select> element by using the "selected" attribute within the <option> tag. This attribute allows you to specify which option should be selected by default when the form is loaded. To do this, you need to check if the option value matches the predefined value and add the "selected" attribute accordingly.
<select name="color">
<option value="red" <?php echo ($color == 'red') ? 'selected' : ''; ?>>Red</option>
<option value="blue" <?php echo ($color == 'blue') ? 'selected' : ''; ?>>Blue</option>
<option value="green" <?php echo ($color == 'green') ? 'selected' : ''; ?>>Green</option>
</select>
Keywords
Related Questions
- Is it advisable to seek help from forums or online communities for specific PHP scripts, or is it better to learn and understand the basics first?
- How can using an array simplify the process of assigning values in PHP functions?
- Are there any alternative methods in PHP to measure image loading times?