我需要选择一个成员,然后再选择一个成员

时间:2019-04-05 07:53:21

标签: php database dropdown

当我的表单打开时,它将出现在数据库的第一行。我想在打开表单时先出现“选择成员”选项,然后再选择成员。

   echo "<select name='member_id' class='form-control' style='width:500px;' >";

    while ($row = $result->fetch_assoc()) {

              unset($member_id, $name);
              $member_id = $row['member_id'];
              $name = $row['name']; 
              echo '<option value="'.$member_id.'">'.$name.'</option>';

  }

    echo "</select>";

2 个答案:

答案 0 :(得分:1)

只需在while循环之前回显一个选项:

echo "<select name='member_id' class='form-control' style='width:500px;' >";

// Add it here and it will be first in the list
echo '<option value="">Choose member</option>';

while ($row = $result->fetch_assoc()) {
    // Your current code

答案 1 :(得分:0)

尝试一下:

   echo "<select name='member_id' class='form-control' style='width:500px;' >";
   echo "<option value='-1' selected='selected'>Choose member</option>";

    while ($row = $result->fetch_assoc()) {

              unset($member_id, $name);
              $member_id = $row['member_id'];
              $name = $row['name']; 
              echo '<option value="'.$member_id.'">'.$name.'</option>';

  }

    echo "</select>";

您可以通过以下方式验证代码:

if($_POST['member_id']) {
//Your code here...