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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26097464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:09:34  来源:igfitidea点击:

How can I Fetch data after selecting from dropdown list

phpmysql

提问by AakkiRock

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.

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

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>

采纳答案by Balrog2000

To get your selected value, you have to reach the $_GETor $_POSTsupertables after the user submits the form.

要获得您选择的值,您必须在用户提交表单后访问$_GET$_POST超级表。

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']; ?>

回答by Malith Ileperuma

You can easily learn how to fetch data from dropdown using this example by clicking HereThis repository contains all the codes

您可以通过单击此处轻松了解如何使用此示例从下拉列表中获取数据此存储库包含所有代码

回答by Alok Gupta

<?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

回答by Indrasinh Bihola

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

首先给你一些id给你选择这样的选项:

<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:

比使用 jquery 和 ajax 这样的东西:

$('#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

getemp_name.php

if(isset($_POST['id'])){
    //fire query using this id and get the name of employee and echo it
    echo $emp_name;
}

回答by Malcolm Kindermans

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:

这取决于你想要什么。如果要在 POST 后查询中使用选定的 ID,可以使用以下查询:

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

Assuming you are using the post method in your form.

假设您在表单中使用 post 方法。

回答by Prashant Tapase

apply below function on onChange

应用以下功能 onChange

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

example

例子