如何获取选定的组合框值并以 html 形式加载其相应的数据


How to get selected combobox value and load its corresponding data in html form

我有从数据库加载值的组合框。我希望当用户从组合框中选择值时,表单应将数据库中的相应值加载到 html 表单中。这是我正在尝试的代码:

<div class="col-lg-6" style="display:none"  id="c" > 
    <form id="aa" action=""   method="post"  >
        <br><br><br>
        <select name="id" id="id" class="span2" style=" width:150px;" onChange="this.form.submit();">
            <?php 
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "valet";
                // Create connection
                $conn = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$conn) {
                    die("Connection failed: " . mysqli_connect_error());
                }
                $arr = array();
                $sql = "SELECT id FROM tbl_user  ";
                $result = mysqli_query($conn, $sql);
                    // echo "User name=" . $row["name"]. "<br>";
            ?>
            <option value="">-select user-</option>
            <?php           
                if (mysqli_num_rows($result) > 0) {
                    // output data of each row
                    while($row = mysqli_fetch_assoc($result)) {
                        $arr[] = $row;
                    }
                    foreach($arr as $key => $row){
                        echo "<option value='".$row["id"]."'>".$row["id"]."</option>";
                        $GLOBALS['a'] = $row["first_name"];
                        $GLOBALS['b'] = $row["last_name"];
                        $GLOBALS['c'] = $row["phone"];
                        $GLOBALS['d'] =  $row["company_id"];
                        $GLOBALS['e'] = $row["register_on"];
                    }
                }
                else {
                    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
                    header('Location: webservices.php');
                }
                mysqli_close($conn);
            ?>
        </select>
        <br><br><br>
        <input type="text" id="first_name" value="<?php $GLOBALS['a']  ?>" name="first_name" style="width: 460px;height: 50px;overflow: hidden;" placeholder="First Name*">
        <br><br><br><br>
        <input type="text" id="last_name" value="<?php $GLOBALS['b']  ?>" name="last_name" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Last Name*">
        <br><br><br><br>
        <input type="text" id="phone" value="<?php $GLOBALS['c']  ?>" name="phone" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Phone*">
        <br><br><br><br>
        <input type="text" id="company_id" value="<?php $GLOBALS['d']  ?>" name="company_id" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Company ID*">
        <br><br><br><br>
        <input type="text" id="register_on" value="<?php $GLOBALS['e']  ?>" name="register_on" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Register On*">
        <br><br><br><br>
        <button name="edituser" id="edituser" type="submit" style="border:0;width:100px;margin-left: 45px;" >
            <img src="images/save.png" alt="">
        </button>
        <button type="submit" style="border:0;width:100px;margin-left: 75px;">
            <img src="images/cancel.png" alt="">
        </button>
    </form> 
</div> 

请帮我如何完成这项任务?

您可以使用 JQuery 来执行此操作。为了记录在案,Marco Mura确实为您提供了解决问题的方法。你只是不想研究问题并在解决方案给你后解决问题。

$("#id").change(function(){
    $.post("www.example.com?val=" + $(this).val(),function(response){
        $("#mydiv").html(response);
    })
})