I need to build a live search but i don't know much about ajax and javascript, but i understand html, css and php.Can someone tell me what i'm doing wrong?
<script>
function liveSearch(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
}
else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getuser.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
<input type="text" onkeypress="liveSearch(this.name)" name="searchWord"/>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'ezcart';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die('Could not connect to the database: ' . $conn->connect_error);
}
$word = $_GET['q'];
$sql= "SELECT prodNam FROM product WHERE prodNam LIKE '$q' ORDER BY prodNam ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div><p>".$row[prodNam]."</p></div>";
}
}
$conn->close();
?>
Change the query
$word = $_GET['q'];
$sql= "SELECT prodNam FROM product WHERE prodNam LIKE '%".$word."%'";
For more information:http://www.w3schools.com/sql/sql_like.asp
first you need to generate a "onkeyup" event in your input to call your javascript function.
example:
<input id="search" name="search" type="text" class="form-control" placeholder="Busca alguna empresa" onkeyup="dinamic(this.value)" autocomplete="off"/>
here you will show the results
<div class="resultados " >
<table class="table hidden" id="resultados" name="resultados">
</table>
</div>
now lets write js function:
function dinamic(cadena) { //here cadena is your input value
var min = 3;
if (cadena.length >= min) { // true when the typed value have 3 or more characters
$.ajax({
type: 'POST', //method to pass the information to live_search.php
url: 'live_search.php',
data: 'cadena=' + cadena, //name of the post variable
success: function (respuesta) {
$('#resultados').html(respuesta); //place to write results
}
});
}
if (cadena.length>min) { //will show or not the table depending on the input value
document.getElementById('resultados').className = 'table';
} else
document.getElementById('resultados').className += ' hidden';
}
lets go with the php:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'ezcart';
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die('Could not connect to the database: ' . $conn->connect_error);
}
$word = $_POST['cadena'];
$sql= "SELECT prodNam FROM product WHERE prodNam LIKE '%".$word."%' ORDER BY prodNam ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<div><p>".$row[prodNam]."</p></div>";
}
}
$conn->close();
remember in my example your php file is called live_search.php
i recommend you tu use PDO instead of procedural php
if you want i can make an example with pdo.