3

I have certainly browsed and just haven't been able to get this seemingly easy answer!

I have a form where I can update records, and I can also edit them by clicking on the edit button I made. When I click to edit them, I obviously want the current settings to be chosen by default. Through $_GET variables, I have prefilled in the text inputs with their correct values from the DB.

However, I have three dropdown lists, and I am unable to get these to choose their correct value and name.

My form, for one example, has a dropdown list of all my users coming from a query of:

SELECT
    users.id AS 'Users ID',
    users.first_name AS 'User First Name',
    users.last_name AS 'User Last Name'
FROM users
ORDER BY users.first_name;

With the following it is correctly showing the list of users by name, and the values are being sent as IDs to the DB

<div class="form-group">
    <label>Choose User</label><br />
    <select name="userid">
    <?php
        while($usersrow = $usersresult->fetch_assoc()) {
        //Display Customer Info
        $output = '<option value="'.$usersrow['User ID'].'">'.$usersrow['User First Name'].' '.$usersrow['User Last Name'].'</option>';

        //Echo output
        echo $output;                   
        }
    ?>
    </select>
</div>

With this as a setup, does anybody know how I can go to my edit field and have my dropdown preselected where the values remain the users ID? I have tried quite a few different ways including concatening with ternary if, and other $_GET variables of $id (for user id) and $name (for user name), but to no avail.

Any help is appreciated!

EDIT

To put it a bit more simply...I am trying to have my dropdown be preselected based off of a $id $_GET variable that matches the value in a dropdown option.

Thanks

1
  • add a selected attribute as appropriate... Commented Jun 20, 2016 at 14:09

5 Answers 5

1

Set the selected attribute for the option that matches the userId being edited:

<div class="form-group">
    <label>Choose User</label><br />
    <select name="userid">
    <?php
        while($usersrow = $usersresult->fetch_assoc()) {
        //Display Customer Info
        if($usersrow['User ID'] == $_GET['userid']){
          $selected = "selected";
        }else{
          $selected = "";
        }
        $output = '<option value="'.$usersrow['User ID'].'" '.$selected.'>'.$usersrow['User First Name'].' '.$usersrow['User Last Name'].'</option>';

        //Echo output
        echo $output;                   
        }
    ?>
    </select>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I found this one to be the best fit for my needs...only one small change was required (but that was just a change of $_GET variable that you couldn't have seen from my code!), so this worked very nicely. Thank you
1

You can try this..

<select name= "USER LIST">
  <option value="">Choose user </option>
  <option value="User1"<?php if($record['User ID']== 'User1'){ echo 'selected'; }?>>USER 1</option>
                       <option value="User2"<?php if($record['User ID']== 'User1'){ echo 'selected'; }?>>USER 2</option>
 </select>

Comments

1
    <option value="<?php echo $row['softcode']; ?>"
     <?php if ($rows['office_type'] == $row['softcode']): ?>  
        selected="selected" 
     <?php endif ?>>
    <?php echo $row['description']; ?>
    </option>

Comments

0

Try something like this:

       if ($usersrow['id']==$_GET['id']) {
             $selected = "selected";
       } else {
             $selected = "";
       }
       $output = '<option '.$selected.' value="'.$usersrow['User ID'].'" >'.$usersrow['User  First Name'].' '.$usersrow['User Last Name'];

    $output.='</option>';

2 Comments

Or... $usersrow['id'] == $_GET['id'] ? $selected = " selected " : $selected=" ";
Thanks for the comment...@Evis was just easier to implement into my code as he had the full code, so I went with his answer. But your logic was good, thank you
0

Take a look a this:

<?php
    while($usersrow = $usersresult->fetch_assoc()) {
    //Display Customer Info
    $output = '<option value="'.$usersrow['User ID'].'"'. $usersrow['User ID']== $_GET['id'] ? "selected" : "" . '" >'.$usersrow['User First Name'].' '.$usersrow['User Last Name'].'</option>';

    //Echo output
    echo $output;                   
    }
?>

The magic part to see if $_GET['id'] matches $userrow['id'], if it does then echo selected else nothing:

$usersrow['User ID']== $_GET['id'] ? "selected" : ""

1 Comment

I tried this one first....but my select options were all empty. I am not sure why this happened, as I made sure the variables were correct, but the logic behind this was good and in fact was very similar to my efforts as well...but neither one quite worked as planned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.