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

Dani AI

Generated

Good call from @diafol that an optgroup is not the right tool here, and ’s approach is on the right track. To make the placeholder both visible by default and un-submittable, add a top option with an empty value that is disabled and selected, and mark the <select> as required. Also escape database output and prefer PDO/mysqli (the old mysql_* API was deprecated and later removed).

Example using PDO and preserving a previously chosen value:

<?php
// PDO connection omitted for brevity
$stmt = $pdo->query('SELECT id, location FROM area ORDER BY location');
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$currentId = isset($_POST['location_id']) && ctype_digit($_POST['location_id'])
    ? (int)$_POST['location_id']
    : null;
?>
<label for="location_id">Location</label>
<select id="location_id" name="location_id" required>
  <option value="" disabled selected>-- Please select --</option>
  <?php foreach ($rows as $r):
        $id   = (int)$r['id'];
        $text = htmlspecialchars($r['location'], ENT_QUOTES, 'UTF-8');
        $sel  = ($id === $currentId) ? ' selected' : '';
  ?>
    <option value="<?=$id?>"<?=$sel?>><?=$text?></option>
  <?php endforeach; ?>
</select>

On submit, always validate server-side as well:

if (empty($_POST['location_id']) || !ctype_digit($_POST['location_id'])) {
    $errors[] = 'Please choose a location.';
} else {
    // Optionally verify the id exists in the DB before using it
}

Notes:

  • selected on the placeholder makes it the initial visible choice; required prevents form submission while that empty value is selected.
  • Use ORDER BY location so the list is predictable.
  • Avoid echoing raw DB values into HTML; htmlspecialchars prevents broken markup and XSS.

Recommended Answers

All 9 Replies

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>"; 
 
 ?>

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.

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>"; 
 
 ?>

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.

Member Avatar for Member #120589

optgroup doesn't work like this.

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

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 :)

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>'; 
 
 ?>
commented: Thanks for helping ... again ! +1

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

Thanks.This worked for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.