0

When my form opens it appears the first row in the database. I want to when opening my form the option Choose member appear first then i select the member.

   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>";
Pravesh
  • 25
  • 5

2 Answers2

1

Just echo an option before your while-loop:

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
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
0

Try this:

   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>";

You can validate your code by writing:

if($_POST['member_id']) {
//Your code here...
Erisan Olasheni
  • 2,395
  • 17
  • 20
  • where do i need to input the if($_POST['member_id']) { for validation – Pravesh Apr 05 '19 at 09:50
  • That has to be in your form processing script, you can just at it to the top of your codes and check if it has been submitted, `// Place this atop of your script. if(isset($_POST)) { if($_POST['member_id']) { // Your codes here } }` – Erisan Olasheni Apr 05 '19 at 11:50
  • If the below answer solve your issue, you should accept it so others know the issue has been resolved. – M. Eriksson Apr 06 '19 at 11:27