在 html select 中将默认选项值留空

Leave default option value empty in html select

我有一个 html select,其中填充了来自使用 foreach 循环的查询的数据。看起来像这样

$client = $wpdb->get_results("SELECT string FROM `file` WHERE 
`code` = 002 AND `status` = 2");

echo 'Filter by client: ';
  echo '<select name="client_list">';
  foreach ($client as $key => $row) {
    $value = $row->string;
    echo 
  '<option value='.$value.'>'
  .$value. '</option>';
  }
  $client = $_GET['client_list'];
  echo '</select>';

它用作过滤器以显示基于 selected 选项值的数据。它过滤的 table 看起来像这样

   |client  | file              | 
   |------  |-------------------|
   |client1 | file00000         |
   |client2 | file00002         |

现在我想将 html 的第一个选项值(默认值)设为空。我该怎么做?

先添加一个空选项:

echo '<select name="client_list"><option value=""></option>';

提交时如果 client_list 为空,你知道有人没有选择任何东西。

你可以用这个

      $client = $wpdb->get_results("SELECT string FROM `file` WHERE 
     `code` = 002 AND `status` = 2");

    echo 'Filter by client: ';
    echo '<select name="client_list"><option value=""></option>';
    foreach ($client as $key => $row) {
        $value = $row->string;
        echo 
       '<option value='.$value.'>'
        .$value. '</option>';
    }
    $client = $_GET['client_list'];
    echo '</select>';