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
How can I Fetch data after selecting from dropdown list
提问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 $_GET
or $_POST
supertables 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
回答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 方法。