doukangbin9698
2014-09-29 10:13
浏览 25
已采纳

如何从下拉列表中选择后获取数据

I want to fetch data from dropdown list. Like if I choose employee id 40 from the dropdown list it will fetch the data from database of that employee and will shown in the textboxes.

this is my dropdown code. please help me how can i get the selected value.

<?php
    $con=mysqli_connect("localhost","root","","hct_db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
?>

 <label>Select Employee ID</label>
     <select class="form-control" name="employee_id">
         <?php 
         $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");

         while($row = mysqli_fetch_array($result)) 
             echo "<option value='" . $row['employee_id'] . "'>" . $row['employee_id'] . "</option>";
         ?>
     </select>

图片转代码服务由CSDN问答提供 功能建议

我想从下拉列表中获取数据。 就像我从下拉列表中选择员工ID 40一样,它将从该员工的数据库中获取数据并显示在文本框中。

这是我的下拉代码。 请帮助我如何获得所选值。

 &lt;?php 
 $ con = mysqli_connect(“localhost”,“root”,“”,“hct_db”  ); 
 //检查连接
 if(mysqli_connect_errno()){
 echo“无法连接到MySQL:”。  mysqli_connect_error(); 
} 
?&gt; 
 
&lt; label&gt;选择员工ID&lt; / label&gt; 
&lt; select class =“form-control”name =“employee_id”&gt; 
&lt;  ?php 
 $ result = mysqli_query($ con,“SELECT employee_id FROM employee order by employee_id”); 
 
 while($ row = mysqli_fetch_array($ result))
 echo“&lt; option value ='”。  $ row ['employee_id']。  “'&gt;” 中 。  $ row ['employee_id']。  “&lt; / option&gt;”; 
?&gt; 
&lt; / select&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • doutiaosu2310 2014-09-29 10:21
    已采纳

    To get your selected value, you have to reach the $_GET or $_POST supertables after the user submits the form.

    So, after the submit, get it as, if you POST:

    <?php 
    $employee_id = $_POST['employee_id']; ?>
    

    If you GET:

    <?php 
    $employee_id = $_GET['employee_id']; ?>
    
    点赞 打赏 评论
  • doushanlv5184 2014-09-29 10:21

    apply below function on onChange

    function myFunction(mySelect) {
            var x = document.getElementById("mySelect").value;
            document.getElementById("demo").innerHTML = "You selected: " + x;
        }
    

    example

    点赞 打赏 评论
  • dongpangfu6322 2014-09-29 10:32
    <?php
    if(isset($_REQUEST['submit']))
    {
      $value=$_POST['employee_id'];
      $query = mysql_query($con,"SELECT employee_name FROM employee where employee_id=$value");
      $result=mysql_fetch_array($query);
      $emp_name=$result['employee_name'];
    }
    ?>
    
    <form action="" method="post" name="form">
     <label>Select Employee ID</label>
       <select class="form-control" name="employee_id">
        <?php $result = mysqli_query($con,"SELECT employee_id FROM employee order by employee_id");
     while($row = mysqli_fetch_array($result)) 
       echo "<option value='" . $row['employee_id'] . "'>" .$row['employee_id'] . "</option>";
     ?>
      </select>
    <input type="submit" name="submit" value="submit">
    </form>
     <input type="text" value="<?=$emp_name?>" name="emp_name"/>
    
    check this code as your need
    
    点赞 打赏 评论
  • dswu26846 2014-09-29 10:33

    First of all give some id to you select option like this:

    <select class="form-control" name="employee_id" id='employee'>
    

    Add you textbox like this:

    <input type='text' name='emp_name' id='emp_name' />
    

    Than use jquery and ajax something like this:

    $('#employee').change(function(){
        var selected_id = $(this).val();
        var data = {id:selected_id};
        $.post('getemp_name.php',data,function(data){
             $('#emp_name').val(data);
        });
    });
    

    getemp_name.php

    if(isset($_POST['id'])){
        //fire query using this id and get the name of employee and echo it
        echo $emp_name;
    }
    
    点赞 打赏 评论
  • dongshi9526 2014-09-29 10:41

    That depends on what you want. If you want to use the selected ID in your query AFTER DOING A POST, you can use the following query:

        "SELECT   *
         FROM     `employee`
         WHERE    `employee`.`employee_id` = " . (int) $_POST['employee_id'] . "
         LIMIT    1"
    

    Assuming you are using the post method in your form.

    点赞 打赏 评论

相关推荐 更多相似问题