0

Hi

I am using the following code to give the user a choice of options in a drop down box:

<?php 
 $query="SELECT location,id FROM area";
$result = mysql_query ($query);
echo "<select name=location_id value=''>location</option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[location]</option>";
}
echo "</select>"; 
 
 ?>

It retrieves all of the options from a MySQl database.

How do I get it, so that the first option is blank, or says "Please Select"??

Thanks

5
Contributors
9
Replies
19
Views
5 Years
Discussion Span
Last Post by Syed_2
0

Try this:

<?php 
$query="SELECT location,id FROM area";
$result = mysql_query ($query);
echo '<select name="location_id">';
echo '<optgroup disabled="disabled" style="margin-top: 1em;">Please select...</optgroup>';

while($nt=mysql_fetch_array($result)){
    echo "<option value={$nt['id']}>{$nt['location']}</option>";
}
echo "</select>"; 
 
 ?>
0

Hi

Thanks for the reply.

It creates a blank option which is first in the list, but it cannot be selected and you only see it when you use the drop down option.

0

Sorry, I put margin-top instead of margin-bottom.

Try now:

<?php 
$query="SELECT location,id FROM area";
$result = mysql_query ($query);
echo '<select name="location_id">';
echo '<optgroup disabled="disabled" style="margin-bottom: 1em;">Please select...</optgroup>';

while($nt=mysql_fetch_array($result)){
    echo "<option value={$nt['id']}>{$nt['location']}</option>";
}
echo "</select>"; 
 
 ?>
0

Thanks again, but it still does the same thing.

The first option from the database shows ie.London, and when you click on the drop down, there is a blank space at the top, but you are unable to select it.

0

optgroup doesn't work like this.

<select ....>
    <option value="" disabled="disabled">Please select...</option>
    <option value="nextone">Next One</option>
    ....
</select>
0

sorry. knew it didn't look right, searched for "select disabled" and copied the code from one of my in development sites without thinking. hope you've accomplished what you were trying to achieve now :)

1

Simply like this:

<?php 
 $query="SELECT location,id FROM area";
$result = mysql_query ($query);
echo '<select name="location_id">';
echo '<option value="">Please Select..</option>';
while($nt=mysql_fetch_array($result)){
echo '<option value="'.$nt['id'].'">'.$nt['location'].'</option>';
}
echo '</select>'; 
 
 ?>

Edited by simplypixie: n/a

Votes + Comments
Thanks for helping ... again !
0

Thanks Emma

I thought I tried that yesterday, or it must have been very similar with something missing!!

Appreciate your help and the input of others

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.