I am trying to get key(primary key) for selected value in my form, so I can add key into joining table. I change my form to autocomplete from drop down list. but do not how to do map with jquery.
This is my php for autocomplete
<?php
if (isset($_POST['type']) && $_POST['type'] == 'faculty_id') {
$type = $_POST['type'];
$name = $_POST['name_startsWith'];
$nameID = $_POST['nameID'];
$query = "SELECT FirstName, LastName, FacultyId FROM Test.Faculty where UPPER(FirstName) LIKE '" . strtoupper($name) . "%'";
$result = mysqli_query($con, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['FirstName'] . ' ' . $row['LastName'];
$nameID = $row['FacultyId'];
array_push($data, $name);
}
mysqli_close($con);
echo json_encode($data);
exit;
}
?>
this is form and jQuery page
<form action="Form.php" method="post">
<input type='text' id="faculty_id" placeholder="Instructor" name="faculty_id" value='' />
<input type="submit" value="submit" name="submit" />
</form>
<script type="text/javascript">
$('#faculty_id').autocomplete({
source: function (request, response) {
$.ajax({
url: 'Form.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
nameID: request.term,
type: 'faculty_id'
},
success: function (data) {
response($.map(data, function (item) {
console.log(item);
//var code = item.split("|");
return {
label: item,
value: item,
data: item
}
}));
}
});
},
autoFocus: true,
minLength: 1,
});
</script>
and php insert query
<?php
if (isset($_POST)) {
$faculty_id = $_POST['faculty_id'];
try {
$stat = $db->prepare("Insert into ATCTest.Schedule
(Faculty )
VALUE (':faculty_id' )");
$stat->bindParam(":faculty_id", $faculty_id);
if ($stat->execute()) {
echo "<h5>Faculty-js: " . $faculty_id . "</h5>";
} else {
echo "Problem!!!";
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}