duanliaoyu8419 2017-03-31 11:39
浏览 212

如何从下拉菜单中选择后自动填充表格

So i'm creating a Customize form so that i can edit my user data using a page. I have a dropdown menu which is contain username from database.

What im gonna do is , when i click the username i want to costumize , all data of the username goes into the form below it ( like email,password,etc )

This pict explain what i wanted to do

This is my dropdown code :

 <h1>Costumize Data</h1>
        <hr>
        <dl><dt>Select data to costumize</dt></dl>
        <select class="form-control" name="userlist">
            <?php
               $servername = "localhost";
               $username = "root";
               $password = "";
               $dbname = "gamestore";

            // Create Connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);

            // Check connection
            if (!$conn) {
                trigger_error("Connection failed: " . mysqli_connect_error());

            }
            //Run Query
            $stmt = "SELECT DISTINCT `username` FROM `login` ORDER BY user_id ASC";
            $result = mysqli_query($conn,$stmt) or die(mysqli_error($conn));
            while(list($category) = mysqli_fetch_row($result)){
                echo '<option value="'.$category.'">'.$category.'</option>';
            }
            mysqli_close($conn);
            ?>

        </select>
        <hr>

And this is my form

<form class="form-horizontal" method="post" action="edit_user.php">
  <div class="form-group">
    <label class="control-label col-sm-4" for="username">Username:</label>
    <div class="col-sm-6">
      <input type="username" class="form-control" name="username" placeholder="Enter username">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-4" for="email">Email:</label>
    <div class="col-sm-6"> 
      <input type="email" class="form-control" name="email" placeholder="Enter email">
    </div>
  </div>
    <div class="form-group">
    <label class="control-label col-sm-4" for="country">Country:</label>
    <div class="col-sm-6"> 
      <input type="country" class="form-control" name="country" placeholder="Enter Country">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-4" for="password">Password:</label>
    <div class="col-sm-6"> 
      <input type="password" class="form-control" name="password" placeholder="Enter password">
    </div>
  </div>
  <div class="form-group"> 

<label class="control-label col-sm-4" for="user_type">Level:</label>
<div class="col-sm-4"> 
<select class="form-control" name="user_type">
    <option value="user">User</option>
    <option value="admin">Admin</option>
</select>
</div>
</div><div class="form-group"> 
    <div class="col-sm-offset-4 col-sm-7">
      <a href="delete_user.php"><button type="submit" class="btn btn-default">Delete</button></a>
      <button type="submit" class="btn btn-primary">Edit</button>
    </div>
  </div>
</form>

Thank you before !

</div>
  • 写回答

1条回答 默认 最新

  • douxianxing5712 2017-03-31 12:01
    关注

    First thing: Never, look, NEVER put the DB rules into frontend. Create other file and put the content there.

    Second: You have one elegant way to do this: Use ajax calling to a php file with to routing/factoring the user object with the data to retrieve to your modal.

    Put a bind into dropdown when event change is triggered, call the ajax function, retrieve the data with your db routine returning the user object and on the success callback put that object content into frontend using

    document.getElementsByTag/ByName/getElementById.value
    

    , etcetera.

    To study what you need, take that: https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号