0

Iam trying to make a live ajax search . When you put a word it automatically shows suggestions bellow just like w3schools. But for some reason my index file and my php doesnt exchange data value or my database doesnt connect for some reason.What i always get is "no country found". Can you check the code for errors? this is the php file :

<?php
include_once('dbconnect.php');
$q = intval($_GET['q']);
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
        $count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
    if($count == "0" || $q == ""){
        $s[] = "No Country found!";
    }else{
                while($row = mysqli_fetch_array($query)){
        $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
                 } 
        }
for($x = 0; $x < $count; $x++) {
          echo $s[$x];
          echo "<br>";
}
?>

and this is my 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 for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for 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>
Sirick
  • 25
  • 10
  • When you debug, what is the value being sent to the server? What is the actual SQL query being executed at runtime? What is the data in `users` and what record(s) do you expect to return? – David Aug 20 '17 at 11:44

2 Answers2

0

Your country name will not in integer. but you convert it into intval change your php file to

include_once('dbconnect.php');
$q = $_GET['q']; //<-----remove intval
    $query = mysqli_query($conn,"SELECT * FROM `users` WHERE userCountry LIKE '%".$q."%'"); 
        $count = mysqli_num_rows($query);
//Replace table_name with your table name and `thing_to_search` with the column you want to search
    if($count == "0" || $q == ""){
        $s[] = "No Country found!";
    }else{
                while($row = mysqli_fetch_array($query)){
        $s[] = $row['userCountry']; // Replace column_to_display with the column you want the results from
                 } 
        }
for($x = 0; $x < $count; $x++) {
          echo $s[$x];
          echo "<br>";
}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
-1

it is important to know the content of the dbconnect.php file because it the one that contains database connection variables. Check if your MySQL Server is up and that all variable are well set.

Mathe Eliel
  • 658
  • 2
  • 6
  • 16