在按钮单击时用 PHP 数据库中的数据填充组合框,并从数据库中加载选定的值


filling combobox with data from php database on button click and load selected value from database

我有 php 表单,使用 js 单击按钮时加载到 HTML 中

        <button id="edituser" type="submit" onclick="toggle_visibility('c');"  style="border:0;width:100px;margin-left: 74px;">
        <img src="images/edituser.png" alt="">
        </button><br><br>

js 代码是:

if(document.getElementById("c").id == id)
{
      e.style.display = 'block';
      document.getElementById("a").style.display='none';
      document.getElementById("b").style.display='none';
      document.getElementById("edituser").style.backgroundImage="url('images/edit_user_hover.png')";
}

带有组合框的 Php 形式如下:

<div class="col-lg-6" style="display:none"  id="c" > 
            <form 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 = "val";
                    // 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;
                               ?>
                <option value="<?php  echo  $row["id"]; ?>" <?php if($_POST["id"]== $row["id"]) {?>  selected="selected" <?php } ?>>  
                <?php  echo  $row["id"];?>
                </option>
            <?php 
    }
} 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" name="first_name" style="width: 460px;height: 50px;overflow: hidden;" placeholder="First Name*">
                <br>
                <br><br><br>
                <input type="text" id="last_name" name="last_name" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Last Name*">
                <br><br><br><br>
                <input type="text" id="phone" name="phone" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Phone*">
                <br><br><br><br>
                <input type="text" id="company_id" name="company_id" style="width: 460px;height: 50px;overflow: hidden;" placeholder="Company ID*">
                <br><br><br><br>
                <input type="text" id="register_on" 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>

在这里,我需要的功能按钮单击组合框应该从数据库中加载值,然后在从组合框中选择值时,表单应该从数据库加载值我是新手,所以请帮助?组合框没有在按钮单击时从数据库中加载值。以及如何从值组合框选择的数据库中加载?

首先,DB连接的PHP代码应该写在另一个文件中,并包含在这里。这是最佳做法。然后使用以下代码检索组合框数据。将查询结果作为这样

获取数组。
$results = array();
while ($row = mysqli_fetch_array($teamQuery)) {
     $results[] = $row;
}

然后在要回显数据的位置迭代数组。

<select name="id" id="id" class="span2" style=" width:150px;" onChange="this.form.submit();">
    <option value="">-select user-</option>
    <?php
      foreach($results as $key => $row){
           echo "<option value='".$row['option_value']."'>".$row['option_name']."</option>";
      }
    ?>
</select>

您应该回显一个选项以将其绘制在 HTML 文件中。供您参考,不建议使用用于显示隐藏数据的方法。如果在初始页面加载时停止检索数据,页面加载速度将更快。只需将 ajax 函数绑定到按钮上,然后在按钮上单击检索组合框数据并显示它。然后在此过程中处理任何其他数据。