Dropdown Box - Use default value not in database[Solved]

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!

Moderator: General Moderators

Post Reply
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Dropdown Box - Use default value not in database[Solved]

Post by christh »

Hi

I current use my dropdown boxes in the following manner

Code: Select all

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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Dropdown Box - Use default value not in database

Post by califdon »

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:

Code: Select all

...
echo "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";
  if($first_name=='Don'>) echo " selected='selected'";
  echo ">$first_name</option>";
}
echo "</select>";
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Dropdown Box - Use default value not in database

Post by requinix »

If you want a "Select..." option to appear, just hardcode it.

Code: Select all

echo "<select name='Names'>";
echo "<option value=''>Select...</option>";
$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";
  if($first_name=='Don'>) echo " selected='selected'";
  echo ">$first_name</option>";
}
echo "</select>";
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Re: Dropdown Box - Use default value not in database

Post by christh »

Thanks Guys

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".

Hopefully that makes sense - Is that possible?

Thanks

Chris
christh
Forum Commoner
Posts: 25
Joined: Sat Jan 16, 2010 5:27 am

Re: Dropdown Box - Use default value not in database

Post by christh »

tasairis wrote:If you want a "Select..." option to appear, just hardcode it.
I see... adding something like - echo "<option value="0" selected='selected'>"Please Enter First Name"</option> after the loop.

Code: Select all

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 "<option value="0" selected='selected'>"Please Enter First Name"</option>
echo "</select>";

Perfect - Many thanks for the help.

Chris
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Dropdown Box - Use default value not in database[Solved]

Post by timWebUK »

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!
Post Reply