Hello everyone, I am learning PHP with mySQL and I want to know if it's possible for me to do a dynamic dropdown box(which I have) that will automatically fill in fields below it such as a textbox. If I select a userID from the dropdown, that selection will fill in the first name, last name, etc... in the textboxes down below. Would that be possible for me to do?

This is what I have for now. Some information will be edited as some information is not meant to be seen

<!DOCTYPE html>
    $servername = 'localhost';
    $username = 'username';
    $password = 'password';
    $dbname = 'database';

    //create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    //check connection
        die("Connection Failed: " . mysqli_connect_error());

    //lets you know that connection is successful
    echo "Connected Successfully". "<br>";

    //sql command to select parts of a table from the database and then sort them
    $sql = "SELECT UserID from table group by UserID";
    //The results are stored here
    $result = mysqli_query($conn, $sql);

    //Http lines of code in php syntax since we are using php after all
    echo "<h1>". "HEADER". "</h1>";
    echo "User ID: &nbsp &nbsp &nbsp &nbsp &nbsp";
    echo "<select name='dropdown'>";
    echo "<option value='User ID'". ">". 'Drop down from Dropdown list'. "</option>";
    //This will go through every entry in the table from the database
    //and then put all the found entries in the dropdown menu.
    while($row = mysqli_fetch_array($result))
        //makes the entry visible in the dropdown menu
        echo "<option value='". $row['UserID']."'>". $row['UserID']. "</option>";
    echo "</select>";

    echo "<br><br>";
    echo "<form method=post>";
    echo "First Name: &nbsp &nbsp". "<input type='text'". "placeholder='Enter first name'". "name='firstName'". ">";
    echo "</form>";

    //closes the connection to the database

Any help/information would be amazing. Thanks guys!

1 Year
Discussion Span
Last Post by hericles

It is possible. What you will want to do is use jQuery and monitor the onchange event of the drop down so your code knows when an option has been selected.
Then you do an http.post to the server with the selected option as the data. Your PHP endpoint will read the data (the user ID) and select the relevant data from the database and return it to the jQuery post which will then display the results on screen.

You can do the same thing purely in PHP but you would need to have a page postback (form submit) for server to know which option was selected.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.