0

i'm trying to make a live search but it's not working. the code was base on the tutorial from you tube. i wonder if this have an issue running on localhost or maybe i really have a problem on the code. this is my first time working on AJAX. whenever i'm typing some text, it doesn't show any result.

  ---index.php----

   <?php include ("includes/connect.php"); ?> 
   <html>
      <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script type="text/javascript">
            function getClients(value){
                $.post("getClients.php",{partialClients:value},function(data)
                 $("#results").html(data);

            });

         }
        </script>
   </head>


   <body>
         <input type="text" onkeyup="getClients(this.value)"></input>
         <br>
         <div id="results"></div>

  </body>
  </html>


   -----getClients.php---

  <?php

     include "includes/connect.php"

     $partialClients= $_POST['partialClients'];
     $client = mysql_query("select * from clients WHERE client like    '%$partialClients%'  ");

     while ($clientArray = mysql_fetch_array($client)) {
        echo "<div>" .$state['client']. "</div>";
    }
   ?>
6
  • whats the error in console Commented Mar 22, 2016 at 7:59
  • in getClients.php this line include "includes/connect.php" should be like this include "includes/connect.php"; Commented Mar 22, 2016 at 8:00
  • check database fields name.. first echo every query and run it in phpmyadmin, if everything is ok than check console Commented Mar 22, 2016 at 8:11
  • select client and printing $state['name']... use SELECT *... and $clientArray['name'] Commented Mar 22, 2016 at 8:17
  • my fault. i changed that already but still not working Commented Mar 22, 2016 at 8:24

1 Answer 1

2

First of all stop using mysql_* extension its deprecated and closed in PHP 7, use mysqli_* or PDO.

You have typo error in your PHP code on following line:

include "includes/connect.php" // terminate with semicolon (;)

You are missing semicolon on above mentioned line.


Second issue is that you are selecting client column only and fetching the name column.

Your SQL Statement only return client column against this query:

select client from clients // only return client column

And what you are fetching in your while loop:

while ($clientArray = mysql_fetch_array($client)) {
    echo "<div>" .$state['name']. "</div>";
}

In above example, $state is Undefined and this should be like:

while ($clientArray = mysql_fetch_array($client)) {
    echo "<div>" .$clientArray['name']. "</div>";
}

And your SQL Statement should be:

SELECT * FROM clients WHERE client LIKE '%$partialClients%'

Use (*) for all columns or add name column in your query.


UPDATE 2:

Modified Code with Deprecated Extension:

<?php
include "includes/connect.php";
$partialClients= $_POST['partialClients'];
$client = mysql_query("SELECT client FROM clients WHERE client LIKE '%$partialClients%'");

while ($clientArray = mysql_fetch_array($client)) {
   echo "<div>" .$clientArray['client']. "</div>";
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

i only need client. i changed the "name" to "client" and still. nothing changed :(
@gracia: $state['client'] should be $clientArray['name']
that should be the column name right? i have table <clients> and columns are <client_ID> , <client> and <image> . i used the client column.
@gracia: if u are using (*) in your query than you need to use $clientArray['client'] inside the while loop..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.