我可以通过调用数据库中的数据来填充select 我将如何使用数据库选择与select值相关的其他数据。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<?php
$conn = new mysqli('localhost', 'root', 'a', 'emp_table')
or die ('Cannot connect to db');
$result = $conn->query("select * from emp");
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['id'];
$name = $row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
</body>
</html>
答案 0 :(得分:0)
试试这个
根据@Nikhil提出的链接
https://www.w3schools.com/xml/ajax_intro.asp
您可以使用AJAX实现此目的。是时候学习AJAX了。
用于连接 如果您正在使用localhost,则无需要求密码。它应该是空白的。
我刚刚将onchange
函数添加到select标记中。因此,无论用户从下拉列表中选择什么,它都将调用函数getRole
并将Id传递给流程页面。处理页面将返回与ID相关的值,并显示在span
标记中。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "emp_table";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title></title>
</head>
<body>
<select name="emp_name" id="emp_name" onchange="getRole(this.value);" >
<option selected disabled>Select</option>
<?php
$result = $conn->query("select * from emp");
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$name = $row['name'];
?>
<option value="<?php echo $id;?>"><?php echo $name;?></option>
<?php }}?>
</select>
<span id="emp_details"></span><!--it will display the emp record from process page-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
function getRole(val){
var id=$('#emp_name').val();
$.ajax({
url:'process.php',
type:'POST',
data:'id='+id,
success:function(data)
{
//alert(data);
$("#emp_details").html(data);
},
});
}
</script>
</body>
</html>
<强> Process.php 强>
<?php
if (isset($_POST['id'])) {
echo $country_id=$conn->real_escape_string(trim($_POST['id']));// here you will get the id of the employee
/*your more code here*/
}
?>