am new with php. Am creating a form with a dropdown list option from my db, i want this option to display the rest of the details in the text field when a user select any. In the DB i have id, employee_name, employee_salary, employee_age.
This my Html file
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script type="text/javascript" src="script/getData.js"></script>
</head>
<body>
<select id="employee" class="form-control" >
<option value="" selected="selected">Select Employee Name</option>
<?php
$sql = "SELECT id, employee_name, employee_salary, employee_age FROM employee";
$resultset = mysqli_query($conn, $sql);
while( $rows = mysqli_fetch_assoc($resultset) ) {
?>
<option value="<?php echo $rows["id"]; ?>"><?php echo $rows["employee_name"]; ?></option>
<?php } ?>
</select>
<br>Craft 1<br>
<input type="text" id="craft_1_points" name="craft_1_points" value="">
<br>Craft 2<br>
<input type="text" id="craft_2_points" name="craft_2_points" value="">
<br>Craft 1<br>
<input type="text" id="craft_3_points" name="craft_3_points" value="">
<br>Craft 2<br>
<input type="text" id="craft_4_points" name="craft_4_points" value="">
</body>
</html>
</center>
<?php include('include/footer.php');?>
I have managed to add the names to the drop down list which is working and i used ajax and java to link. But when i select any option e.g. Tiger Nicxin it supposed to fill the text field with the rest info of the selected name, id,age and salary. It's not working please what do i have to do.
JS file
$(document).ready(function(){
$("#employee").change(function() {
var id = $(this).find(":selected").val();
var dataString = 'empid='+ id;
$.ajax({
url: 'getlist.php',
dataType: "json",
data: dataString,
cache: false,
success: function(empData) {
if(empData) {
$("#errorMassage").addClass('hidden').text("");
$("#recordListing").removeClass('hidden');
$("#empcraft_1_points").val(empData.id);
$("#empcraft_2_points").val(empData.employee_name);
$("#empcraft_3_points").val(empData.employee_age);
$("#empcraft_4_points").val("$"+empData.employee_salary);
} else {
$("#recordListing").addClass('hidden');
$("#errorMassage").removeClass('hidden').text("No record found!");
}
}
});
})
});
Ajax file
<?php
include_once("include/db_connect.php");
if($_REQUEST['empid']) {
$sql = "SELECT id, employee_name, employee_salary, employee_age
FROM employee
WHERE id='".$_REQUEST['empid']."'";
$resultSet = mysqli_query($conn, $sql);
$empData = array();
while( $emp = mysqli_fetch_assoc($resultSet) ) {
$empData = $emp;
}
echo json_encode($empData);
} else {
echo 0;
}
?>
please help me with solution