I'm starting with ajax. I have an input that generates a search with ajax in my mysql database, but it does not return any results and the console does not show any error
$(document).ready(function() {
$("#resultadoBusqueda").html('<p>JQUERY VACIO</p>');
});
function buscar() {
var textoBusqueda = $("input#busqueda").val();
if (textoBusqueda != "") {
$.post("buscar.php", {valorBusqueda: textoBusqueda}, function(mensaje) {
$("#resultadoBusqueda").html(mensaje);
});
} else {
$("#resultadoBusqueda").html('<p>JQUERY VACIO</p>');
};
};
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<form accept-charset="utf-8" method="POST">
<input type="text" name="busqueda" id="busqueda" value="" placeholder="" maxlength="30" autocomplete="off" onKeyUp="buscar();" />
</form>
<div id="resultadoBusqueda"></div>
And this is the code of the search.php file
<?php
require('../conexion.php');
$consultaBusqueda = $_POST['valorBusqueda'];
$caracteres_malos = array("<", ">", "\"", "'", "/", "<", ">", "'", "/");
$caracteres_buenos = array("& lt;", "& gt;", "& quot;", "& #x27;", "& #x2F;", "& #060;", "& #062;", "& #039;", "& #047;");
$consultaBusqueda = str_replace($caracteres_malos, $caracteres_buenos, $consultaBusqueda);
$mensaje = "";
if (isset($consultaBusqueda)) {
$consulta = mysqli_query($conexion, "SELECT * FROM respuestos
WHERE maquina COLLATE UTF8_SPANISH_CI LIKE '%$consultaBusqueda%'
OR repuesto COLLATE UTF8_SPANISH_CI LIKE '%$consultaBusqueda%'
OR CONCAT(maquina,' ',repuesto) COLLATE UTF8_SPANISH_CI LIKE '%$consultaBusqueda%'
");
$filas = mysqli_num_rows($consulta);
if ($filas === 0) {
$mensaje = "<p>No hay ninguna maquina y/o repuesto</p>";
} else {
echo 'Resultados para <strong>'.$consultaBusqueda.'</strong>';
while($resultados = mysqli_fetch_array($consulta)) {
$maquina = $resultados['maquina'];
$repuesto = $resultados['repuesto'];
$ref = $resultados['ref'];
$mensaje .= '
<p>
<strong>maquina:</strong> ' . $maquina . '<br>
<strong>repuesto:</strong> ' . $repuesto . '<br>
<strong>Ref.:</strong> ' . $ref . '<br>
</p>';
};
};
};
echo $mensaje;
?>
This is the SQL of my table with 1 record
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE TABLE 'repuestos' (
'id' int(11) NOT NULL,
'ref' varchar(100) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL,
'tipo' varchar(100) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL,
'maquina' varchar(400) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL,
'repuesto' varchar(400) CHARACTER SET utf8 COLLATE utf8_spanish_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO 'repuestos' ('id', 'ref', 'tipo', 'maquina', 'repuesto') VALUES
(1, 'BM-154D80', 'Sierra', 'Sierra BM-180', 'Cinta 1600 Hueso');
ALTER TABLE 'repuestos'
ADD PRIMARY KEY ('id');
ALTER TABLE 'repuestos'
MODIFY 'id' int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
And here the connection file, this file has worked well with another table for a year so I do not think it's the problem
<?php
function dbConnect (){
$conn = null;
$host = 'Localhost';
$db = '****';
$user = '*****';
$pwd = '*****';
try {
$conn = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pwd);
}
catch (PDOException $e) {
echo '<p>Error al conectar a la base de datos</p>';
exit;
}
return $conn;
}
?>