Hi, I have a dynamic search and it's connected to a Base de Datos
the problem is that I have a problem calling the function js
, I do not know if I'm calling it right.
Here I attach the code, it is divided into 3 documents.
Code:
index.html
<html>
<body>
<head>
<script type="text/javascript" src="busqueda.js"></script>
</head>
<input type="text" id="busqueda" />
<div id="resultado"></div>
</body>
</html>
busqueda.js
$(document).ready(function(){
var consulta;
//hacemos focus al campo de búsqueda
$("#busqueda").focus();
//comprobamos si se pulsa una tecla
$("#busqueda").keyup(function(e){
//obtenemos el texto introducido en el campo de búsqueda
consulta = $("#busqueda").val();
//hace la búsqueda
$.ajax({
type: "POST",
url: "buscar.php",
data: "b="+consulta,
dataType: "html",
beforeSend: function(){
//imagen de carga
$("#resultado").html("<p align='center'><img src='ajax-loader.gif' /></p>");
},
error: function(){
alert("error petición ajax");
},
success: function(data){
$("#resultado").empty();
$("#resultado").append(data);
}
});
});
});
search.php
<?php
$buscar = $_POST['b'];
if(!empty($buscar)) {
buscar($buscar);
}
function buscar($b) {
$con = mysql_connect('localhost','root', 'Engineringg96');
mysql_select_db('bdutc', $con);
$sql = mysql_query("select df.idficha, ml.clas, ml.titulo, ml.autor, ml.editorial, ml.fecha, ml.pag, ml.asignatura, ml.tema,ml.isbn, cc.categoria, ca.carrera, cu.cuatrimestre, cp.plantel
FROM dficha ta
JOIN dficha df ON ta.idficha=df.idficha
LEFT JOIN mlibro ml ON ta.idlibro=ml.idlibro
LEFT JOIN ccategoria cc ON ta.idcategoria=cc.idcategoria
LEFT JOIN ccarrera ca ON ta.idcarrera=ca.idcarrera
LEFT JOIN ccuatrimestre cu ON ta.idcuatrimestre=cu.idcuatrimestre
LEFT JOIN cplantel cp ON ta.idplantel=cp.idplantel
where titulo LIKE '%".$b."%' or clas LIKE '%".$b."%'" ,$con);
$contar = mysql_num_rows($sql);
if($contar == 0){
echo "No se han encontrado resultados para '<b>".$b."</b>'.";
}else{
while($row=mysql_fetch_array($sql)){
$nombre = $row['titulo'];
$id = $row['clas'];
echo $id." - ".$nombre."<br /><br />";
}
}
}
?>