Leave default option value empty in html select

我们两清 提交于 2019-12-24 09:49:39

问题


I have an html select that is populated with DATA from a query using a foreach loop. It looks something like this

$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>';

It serves as a filter to display data based on the selected option value. The table that it filters looks somehting like this

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

Now I want to make the first option value (the default value) of the html empty. How do I do this?


回答1:


you can use this

      $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>';



回答2:


Add an empty option first:

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

on submit if client_list is empty you know someone didn't choose anything.



来源:https://stackoverflow.com/questions/46582289/leave-default-option-value-empty-in-html-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!