M-am folosit de acest tutorial pentru a cauta live intr-un tabel din mysql cu ajutorul unui input.
webslesson.info/2017/04/dynamic-autocomplete-search-using-bootstrap-typeahead-with-php-ajax.html
O sa sintetizez codul:
Cod: Selectaţi tot
<input type="text" name="country" id="country" class="form-control input-lg" autocomplete="off" placeholder="Type Country Name" />
Cod: Selectaţi tot
<script>
$(document).ready(function(){
$('#country').typeahead({
source: function(query, result)
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{query:query},
dataType:"json",
success:function(data)
{
result($.map(data, function(item){
return item;
}));
}
})
}
});
});
</script>
Cod: Selectaţi tot
<?php
//fetch.php
$connect = mysqli_connect("localhost", "root", "", "testing");
$request = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM countries WHERE name LIKE '%".$request."%'
";
$result = mysqli_query($connect, $query);
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row["name"];
}
echo json_encode($data);
}
?>
Ca sa nu mai creez un alt imput, ideal ar fi sa aduc id-ul tarii alese direct in value inputului id="country"
Multumesc!