user2216351
user2216351

Reputation: 31

Auto suggest text box using PHP and jQuery

I am trying to do an auto suggest text box using PHP and jQuery however, all the jQuery I am using, basing on the internet has already deprecated. I am not sure if my code is the one not working or the jQuery. Could anyone help me please? :) Thank you in advance!

<?php
   session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>jQuery Autocomplete Plugin</title>
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type='text/javascript' src="js/jquery.autocomplete.js"></script>
    <link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
    <script type="text/javascript">
      $().ready(function() {
        $("#users").autocomplete("autoCompleteMain.php", {
          width: 260,
          matchContains: true,
          selectFirst: false
        });
      });
    </script>
  </head>
  <body>
    <h2 id="banner">Autocomplete</h1>
    <div id="content">
      <form autocomplete="off">
        <p>
          Enter Username <label>:</label>
          <input type="text" name="users" id="users" />
        </p>
        <input type="submit" value="Submit" />
      </form>
    </div>
  </body>
</html>

PHP:

$host="localhost"; // Host name
$username="root"; // Mysql username
$password="aaaa"; // Mysql password
$db_name="maptemp"; // Database name

$con = mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db_name, $con) or die(mysql_error());

$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select username from users where username LIKE '%$q%'";
$rsd = mysql_query($sql);

while($row = mysql_fetch_array($rsd)) {
  $cname[] = $row['username'];
}

echo json_encode($cname);

Upvotes: 1

Views: 2191

Answers (3)

user2514989
user2514989

Reputation: 1

No need to encode the result in JSON otherwise at front have you parse it. so make a html in php and echo it so you get result in html format.

Upvotes: 0

RafH
RafH

Reputation: 4544

You should declare your array in your PHP script : $cname = array();

And as Dipesh Parmar said, jQuery uses "term" parameter.

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

Replace

$q = strtolower($_GET["q"]);

with

$q = strtolower($_GET["term"]);

because as i know jQuery auto Complete pass term.

Upvotes: 1

Related Questions