表单ajax.php
包含一个dropdown
和一个textarea
。一个用于从数据库获取的员工姓名。我需要在更改下拉项目时显示员工的详细信息。这里是我的代码
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#demo").change(function() {
$empname = $('#demo').val();
$.ajax({
type: "post",
url: "display.php",
data: "empname=" + $empname,
success: function(data) {
if (data == '') {
alert('no data');
} else {
$("#desc").html(data);
}
}
});
});
});
</script>
</head>
<body>
<?php
$con= mysqli_connect("localhost", "root", "", "process");
if(!$con){
echo 'not connected';
}
?>
<label for="selector1" class="col-sm-2 control-label">Assignee</label>
<div class="col-sm-8"><select name="emp" id="demo" class="form-control1">
<?php
$sql=mysqli_query($con,"select * from employee");
while($name=mysqli_fetch_array($sql)) {
?>
<option value="<?php echo $name['id'];?>"><?php echo $name['empname'];?></option>
<?php } ?>
</select></div>
description <textarea id="desc"></textarea>
<input type="submit" name="sub" value="click" id="bt">
</body>
</html>
我的下一页即。display.php
包含以下代码,
<?php
$con= mysqli_connect("localhost", "root", "", "process");
if(!$con){
echo 'not connected';
}
if( $_REQUEST["data"] ){
$name = $_REQUEST['data'];
$sql=mysqli_query($con,"select * from employee where id='$empname'");
$name=mysqli_fetch_array($sql);
echo $name['empname'];
}
答案 0 :(得分:1)
问题是您要在empname
中传递数据并使用data
进行重新更改:
<强> Display.php的强>
<?php
$con= mysqli_connect("localhost", "root", "", "process");
if(!$con){
echo 'not connected';
}
if( $_REQUEST["empname"] ){ //change here
$empname= $_REQUEST['empname'];//change here
$sql=mysqli_query($con,"select * from employee where id='$empname'");
$name=mysqli_fetch_array($sql);
echo $name['empname'];
}