-4

I have a drop-down in html which is being rendered from php template. I can edit drop-down choice which takes usernames from DB, but how make it "selected" (what I have choose before) before editing?

Code for the template is below.

        $result = $conn->query("select username from users");
    echo "<html>";
    echo "<body>";
    echo "<select name='workers' >";
    while ($row = $result->fetch_assoc()) {
                  unset($username);
                  $username = $row['username'];
    echo '<option value=" '.$username.'"  >'.$username.'</option>';}
    echo "</select>";
    echo "</body>";
    echo "</html>";
    ?> 

Please help!

2

1 Answer 1

0

You need to give to name to select tag and get that as that name like :

<?php
       $conn = new mysqli($servername, $username, $password, $dbname) or die ('Cannot connect to db');

            $result = $conn->query("select username from users");
        echo "<html>";
        echo "<body>";
        echo "<select name='drpdown'>";
        while ($row = $result->fetch_assoc()) {
            unset($username);
            $username = $row['username'];
            echo '<option value=" '.$username.'"  >'.$username.'</option>';
        }
        echo "</select>";
        echo "</body>";
        echo "</html>";
    ?>

get the post variable like :

print_r($_POST['drpdown']);
Sign up to request clarification or add additional context in comments.

Comments

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.