javascript - Ajax search doesnt connect to database -
iam trying make live ajax search . when put word automatically shows suggestions bellow w3schools. reason index file , php doesnt exchange data value or database doesnt connect reason.what "no country found". can check code errors? php file :
<?php include_once('dbconnect.php'); $q = intval($_get['q']); $query = mysqli_query($conn,"select * `users` usercountry '%".$q."%'"); $count = mysqli_num_rows($query); //replace table_name table name , `thing_to_search` column want search if($count == "0" || $q == ""){ $s[] = "no country found!"; }else{ while($row = mysqli_fetch_array($query)){ $s[] = $row['usercountry']; // replace column_to_display column want results } } for($x = 0; $x < $count; $x++) { echo $s[$x]; echo "<br>"; } ?>
and index.php file :
<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <link rel="stylesheet" href="/resources/demos/style.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <script> function showcountry(str) { if (str == "") { document.getelementbyid("txthint").innerhtml = ""; return; } else { if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange = function() { if (this.readystate == 4 && this.status == 200) { document.getelementbyid("txthint").innerhtml = this.responsetext; } }; xmlhttp.open("get","indexsearchquery.php?q="+str,true); xmlhttp.send(); } } </script> <input id="search-box" name="q" type="text" autocomplete="off" placeholder="search country..." onchange="showcountry(this.value)" /> <input type='image' name='search' id="search-icon" value='submit' src="search-icon.png" > <p style="color:white;">suggestions: <span id="txthint" ></span></p>
your country name not in integer. convert intval
change php file to
include_once('dbconnect.php'); $q = $_get['q']; //<-----remove intval $query = mysqli_query($conn,"select * `users` usercountry '%".$q."%'"); $count = mysqli_num_rows($query); //replace table_name table name , `thing_to_search` column want search if($count == "0" || $q == ""){ $s[] = "no country found!"; }else{ while($row = mysqli_fetch_array($query)){ $s[] = $row['usercountry']; // replace column_to_display column want results } } for($x = 0; $x < $count; $x++) { echo $s[$x]; echo "<br>"; }
Comments
Post a Comment