I'm doing a kind of search engine, in which while you press a key, an AJAX request is made to consult it in the DB. The point is that I still do not know much about AJAX and it does not work for me. At the moment I am testing with any text instead of consulting the database.
HTML:
<input placeholder="Buscar..." id="buscador" type="text">
<p class="resultados"></p>
JAVASCRIPT:
$('#buscador').on('keypress', () => {
let busqueda = $('#buscador').val();
// Ajax para recibir los 'posts'
$.ajax({
type: 'POST',
data: busqueda,
url: 'ajax/indexAjax.php',
success: function(respuesta) {
$('.resultados').html(respuesta);
}
});
});
PHP:
<?php
$busqueda = $_POST['busqueda'];
$data = "";
if(isset($busqueda)){
$data = $busqueda;
}else{
$data = "No existe";
}
return $data;
?>