PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
echo "<select name="Names">";
$result = mysql_query ( "SELECT * FROM name ORDER BY first_name");
while($row = mysql_fetch_array($result));
{
$name_id=$row['id'];
$first_name=$row['first_name'];
echo "<option value=$name_id>$first_name</option>";
}
echo "</select>";
Which all works fine.
To date, I've been setting the default value for the drop-down by making an entry in the database that will appear at the top of the ordered list i.e. adding (Select a name) to the first_name col.
Whilst this works, it is obviously the wrong way of setting the default value and it certainly throws up odd values when the database is ordered in any way other than alphabetical.
Could somebody please tell me how to set a default value for a drop-down box without making a needless entry into the database?
Many thanks
Chris
Last edited by christh on Mon Sep 13, 2010 4:44 am, edited 1 time in total.
You can make any <option> the default value by adding "selected='selected'" within the <option> tag. This is easy to do in your PHP code. Let's say you want your html form to open with the value "Don" selected, but allow the user to select something else:
Looking at your suggestions, I don't think it gives the solutions I'm after.
Both those suggestions require the name "Don" (or any other name) to be in the database - what I am hoping to achieve is to have something like "Please Select First Name" as the default option without having to create a database first name entry of "Please Select First Name".
Just a bit of advice, make sure you use " " marks consistently throughout, I've noticed sometimes you have used inverted commas and sometimes you don't use anything at all!