You cannot generate multiple JSON encodes; that gets you no valid JSON, and jQuery will ignore it.
while ($row = mysqli_fetch_assoc($sql)) {
$row_array['PNo'] = $row['PNo'];
$row_array['PName'] = $row['PName'];
array_push($return_arr, $row_array);
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
}
The echo json_encode($return_arr);
must happen only once after the mysqli_close
:
array_push($return_arr, $row_array);
}
/* Toss back results as json encoded array. */
echo json_encode($return_arr);
Also, just in case, output the JSON content-type headers:
}
/* Toss back results as json encoded array. */
Header("Content-Type: application/json; charset=UTF-8");
die(json_encode($return_arr));
Lastly, you report this error in your comment:
<b>Warning</b>: include() [<a href='function.include'>function.include</a>]: Failed opening '../_php/search.php' for inclusion (include_path='.:/usr/local/php-5.3.29/share/pear')
This error can be caused by one thing only: you must be including from PHP the search.php
script. I.e. in some of your PHP scripts there is a
include '../_php/search.php';
This must not be done - search.php
must be an independent PHP script and it should return a valid JSON if you call its URL from a browser like this:
http://<yoursite>/_php/search.php?term=%
i.e., it should return something like
[["PnoNumber1":"PNameNumber1"],["PnoNumber2":"PNameNumber2"]]
File organization (there's more than one way to do it)
The HTML file can be wherever you want, let's say it is in /_html/page.html
This file includes the necessary JS code which lives in /_js, so /_js/jquery.min.js and so on.
The PHP is called by the Javascript code inside the HTML as is detailed in another answer. When you include the 'search.php' URL in your Javascript, this generates a new browser call which has a lifecycle of its own in AJAX. This call must be able to "live" by itself, so it must work also if you use it in your browser's address bar. Otherwise it will not work in the script either.
So to test that the script is working (of course you can also use tools such as Firebug) you can invoke it from the browser:
http://<yoursite>/_php/search.php?term=%
The script must then connect to the database and perform the query, returning a valid JSON in the format that Autocomplete expects. So in answer to the call above you must see a valid JSON in the browser window.
The .autocomplete function is correctly bound with $('#auto') and the input field has indeed an id of 'auto'. So the call will be generated. The URI you put in the javascript must be valid with reference to the javascript, so if the form lives in /_html/searchpage.html, and the script in /_php/search.php, the relative URI will be ../_php/search.php (of course if the form is in /_html/forms/search.html, you will need ../../_php/search.php and so on).