doulongti5932 2017-12-10 05:28
浏览 40

根据SQL查询在下拉列表html中设置默认项?

Would this be the proper way to add a default value to a drop down menu it HTML?

$vendor_name is determine by a query to get an array of results; that is iterated through to create table rows in HTML. So this value changes dependent on the iteration.

<option value='$vendor_name'>$vendor_name</option>";
while($row = mysqli_fetch_array($result))
{
    echo "<option value='".$row['vendor_id']."'>".$row['name']."</option>";
}

When I attempt this it shows the default value; however, the table that is being created here is used to update an SQL table. If i change any other value in the row related to the $vendor_name without changing the $vendor_name it will not update. Is this because I set the default value?

  • 写回答

2条回答 默认 最新

  • duanla8800 2017-12-10 06:14
    关注

    Below is the correct way of setting default item in DropDown with PHP.

    <?php
    while ($row = mysqli_fetch_array($result)) {
      $selected = '';
      if ($vendor_name == $row['name']) {
        $selected = ' selected';
      }
      echo "<option value='" . $row['vendor_id'] . "'" . $selected . ">" . $row['name'] . "</option>";
    }
    ?>
    
    评论

报告相同问题?