I'm developing an application for college, and I came across the following problem:
I have to search used autocomplete passing as parameter the name of the doctor, so retrieve the id from it and save it to the bank. I've tried it anyway but I can not retrieve the id , only autocomplete works.
Example
In PHP
<div class="form-group">
<label>Profissional</label>
<input type="text" name="prof" value="" class="form-control" placeholder="Insira o nome do Profissional." id="txtProf" class="typeahead">
<input type="hidden" name="idprof" value="" class="form-control" id="idprof" >
</div>
<div class="form-group">
<label >Procedimento</label>
<input type="text" name="proced" value="" class="form-control" placeholder="Insira o nome do Procedimento." id="txtProc" class="typeahead">
</div>
In JavaScript:
$(document).ready(function () {
$('#txtProf').typeahead({
source: function (query, result) {
$.ajax({
url: "../classes/db/server.php",
data: 'query=' + query,
dataType: "json",
type: "POST",
success: function (data) {
result($.map(data, function (item) {
return item;
}));
}
});
}
});
});
And in the search file (server.php):
$keyword = strval($_POST['query']);
$search_param = "{$keyword}%";
$conn =new mysqli('localhost', 'root', '' , 'clinicanova');
$sql = $conn->prepare("SELECT pf.nome FROM tb_pessoa pf, tb_profissional prof
WHERE pf.id = prof.tb_pessoa_id
and prof.status = 'A'
and pf.nome LIKE ?");
$sql->bind_param("s",$search_param);
$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$countryResult[] = $row["nome"];
}
echo json_encode($countryResult);
}
$conn->close();
I wanted a way to complete the ID field with the data that the user selected.
SOON I GOT TO REDUCE THE ID SO MISSING TO GO WITHIN HIDDEN knows how I can do this because adding direct to the result does not work , MY JAVASCRIPT IS SO NOW:
<script>
$(document).ready(function () {
$('#txtProf').typeahead({
source: function (query, result) {
$.ajax({
url: "../classes/db/server.php",
data: 'query=' + query,
dataType: "json",
type: "POST",
success: function (data) {
result($.map(data, function (item) {
return item.nome;
$('#idprof').val(item.id);
}));
}
});
}
});
});
HOW DO I INSERT THIS "item.id" into an input