peterj
peterj

Reputation: 72

Set selected item in combobox to "please select..."

I'd like to add "Please select..." as the first option in my combobox. See code below - any suggestions?

Regards, Peter

<?php

mysql_connect ("localhost", "username", "password"); 
mysql_select_db('mysqldb');

$sql = "SELECT data_id FROM projects";
$result = mysql_query($sql);

echo "<select name='data_id '>";
$sql[0] = 'Please select...';
while ($row = mysql_fetch_array($result)) 
{
    echo "<option value='" . $row['data_id '] . "'>" . $row['data_id'] . "</option>";
}
echo "</select>";

?>

Upvotes: 0

Views: 706

Answers (2)

Dehalion
Dehalion

Reputation: 757

Why not just replace your $sql[0] = 'Please select...'; line (which by the way does nothing useful whatsoever) by this line:

echo "<option selected disabled>Please select...</option>";

The selected keyword is even optional if this is the first option you specify.

Upvotes: 0

user576126
user576126

Reputation:

replace $sql[0] = 'Please select...';
to this echo '<option value="" disabled>Please select...</option>';

Upvotes: 1

Related Questions