我正在获取所选选项值的ID,但是表数据不会随着选项更改而显示。我没有收到错误,也找不到错误。
dashboard.php
<select id="employee">
<option value="" selected="selected"></option>
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "test2";
$lastId="";
//create connection
$con = mysqli_connect($host, $user, $pass, $db_name);
$sql = "SELECT asset_type,department,cost FROM track_data";
$resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($conn));
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
<option value="<?php echo $rows["id"]; ?>"><?php echo $rows["asset_type"]; ?></option>
<?php } ?>
</select>
<div id="display" style="color: black">
<div class="row" id="heading" style="color: black"><h3><div class="col-sm-4"><strong>Employee Name</strong></div><div class="col-sm-4"><strong>Age</strong></div><div class="col-sm-4"><strong>Salary</strong></div></h3></div><br>
<div class="row" id="records" style="color: black"><div class="col-sm-4" id="emp_name"></div><div class="col-sm-4" id="emp_age"></div><div class="col-sm-4" id="emp_salary"></div></div>
</div>
<script type="text/javascript">
$(function () {
// $("#show_table").show();
$(document).ready(function(){
// code to get all records from table via select box
$("#employee").change(function() {
var id = $(this).find(":selected").val();
var dataString = 'id='+ id;
$.ajax({
url: 'getEmployrr.php',
dataType: "json",
data: dataString,
cache: false,
success: function(employeeData) {
if(employeeData) {
$("#emp_name").text(employeeData.asset_type);
$("#emp_age").text(employeeData.department);
$("#emp_salary").text(employeeData.cost);
$("#records").show();
} else {
$("#heading").hide();
$("#records").hide();
}
}
});
})
});
});
</script>
getEmployrr.php
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db_name = "test2";
$lastId="";
//create connection
$con = mysqli_connect($host, $user, $pass, $db_name);
if($_REQUEST['id']) {
$sql = "SELECT asset_type,department,cost FROM track_data WHERE id='".$_REQUEST['id']."'";
$resultset = mysqli_query($con, $sql) or die("database error:". mysqli_error($con));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data = $rows;
}
echo json_encode($data);
} else {
echo 0;
}
?>
答案 0 :(得分:1)
在第一个SQL查询中:
$sql = "SELECT asset_type,department,cost FROM track_data";
您没有选择id
,请尝试将其添加到查询中:
$sql = "SELECT id,asset_type,department,cost FROM track_data";