0
votes

This might be very easy for some of you but very hard for me since first time doing it.

By looking at some examples on the web, I ended up with the code below for auto-suggestion example but the code doesn't work.

Thanks

HTML

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
            $('#textbox_postcode').autocomplete(
            {
                source: 'search-db.php',
                minLength: 3
            });
        });
    </script>
</head>

<body>
    <form action="search.php" method="post">
        <input type="text" id="textbox_postcode" value="" />&nbsp;&nbsp;&nbsp;<input type="submit" value="Search" />
    </form>
</body>
</html>

PHP

$keyword = ltrim(strtolower(strip_tags($_GET['keyword'])));

if (! $keyword) return;

$host = 'localhost'; $user = 'root'; $pswd = ''; $dtbs = 'geomaps';

$host_conn = mysql_connect($host, $user, $pswd); $dtbs_conn = mysql_select_db($dtbs);

$return = array();

$sql = "SELECT id, postcode FROM postcodes WHERE postcode LIKE '$keyword%' ORDER BY postcode"; $run = mysql_query($sql);

if (@mysql_num_rows($run) == 0) return;

while ($records = mysql_fetch_array($run, MYSQL_ASSOC)) { $return[] = $records; }

echo json_encode($return);

1
When I first load the page I get this error : Uncaught TypeError: Object [object Object] has no method 'autocomplete'. When I type something, nothing happens. - BentCoder
isn't autocomplete in jQuery UI? - Graham Clark
Yes, it is. So at the very least he needs to include the jQuery UI js and css files. - jmoerdyk
Ok I added jquery-ui.css, jquery.min.js and jquery-ui.min.js to my html. I changed echo json_encode($return); with return json_encode($return); (same with echo). Error is gone but I still cannot get the result from database. Any idea? - BentCoder
keep the echo json_encode($return). Does the PHP page print the JSON object? - Doa

1 Answers

1
votes

Try changing $_GET['keyword'] to $_GET['term']