0
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 recover the id of the same and save in the bank. I have tried every way but I can not recover the id, only the 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();
Wanted a way to complete the ID field with the data the user selects.
READY I WAS ABLE TO RESCUE THE ID SO MISSING POPULATE INSIDE HIDDEN knows how I can do it because adding straight to the result does not work MY JAVASCRIPT IS LIKE THIS 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
First you have to retrieve and store the ID in the variable
countryResult
. Yourselect
is only returning thepf.nome
– Valdeir Psr
sorry to be unaware, I’m getting used to the language now I don’t know if I did right but I changed the code to: if ($result->num_rows > 0) { while($Row = $result->fetch_assoc()) { $countryResult[] = $Row["name"]; $countryResult[] = $Row["id"]; }
– Jose Selmar Junior
I did not understand how this logic works, to pass and how to recover there js and work in the form
– Jose Selmar Junior