Is there any way to set the selected item in a drop down box using the following 'type' code?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
:) I hope this function help others
Simple and easy to understand example by using ternary operators to set selected value in php
Simple way
You need to set the selected attribute of the correct option tag:
Your PHP would look something like this:
I usually find it neater to create an array of values and loop through that to create a dropdown.
You mark the selected item on the
<option>
tag, not the<select>
tag.So your code should read something like this:
You can make this less repetitive by putting all the month names in an array and using a basic
foreach
over them.A Simple Solution: It work's for me