Page 1 of 1

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

Posted: Sat Sep 11, 2010 9:54 am
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

Re: Dropdown Box - Use default value not in database

Posted: Sat Sep 11, 2010 11:46 am
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>";

Re: Dropdown Box - Use default value not in database

Posted: Sat Sep 11, 2010 7:45 pm
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>";

Re: Dropdown Box - Use default value not in database

Posted: Mon Sep 13, 2010 4:32 am
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

Re: Dropdown Box - Use default value not in database

Posted: Mon Sep 13, 2010 4:43 am
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

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

Posted: Mon Sep 13, 2010 7:12 am
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!