Hey All, I am trying to write a php script and would like to have an autosuggest text box with data from a mysql database. This would be for designating a town in a form.
The sql query I would use would be.

<?php
$connection = @mysql_connect($server,$dbusername,$dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection) or die(mysql_error());
$sql = "SELECT DISTINCT `Town` FROM '".$Table_Name."' ";
$result = @mysql_query($sql,$connection)or die(mysql_error());
?>

Then there would be a second autosuggest text box that would only select cemeteries in the town from the first box.

I have found a few tutorials on the web but can't seem to get them to work with a sql query.

The tutorial that I have been trying to use is located .

The demo works fine but I cant get the results to show up from the sql query above. When I tested the sql query in the test.php file that I made it seems to work fine.

Does anyone have any suggestions??? Anyhelp is appreciated.

Dani AI

Generated

A few practical points that make autosuggest work reliably (and the things that commonly trip people up): server output must match exactly what the JavaScript expects, identifier/field names in your SQL must be correct, and any PHP warning/HTML injected before the response will break the client-side parser. solved a classic case of a misnamed field — that kind of mismatch is the first thing to check.

Quick troubleshooting checklist:

  • Call the autocomplete URL directly in a browser (for example autocomplete.php?q=foo) to see the raw response.
  • Use the browser Network panel to confirm the XHR returns 200 and the body contains just the suggestion data.
  • Make sure the AJAX parameter name (q or whatever your JS sends) matches the server code.
  • Do not wrap table names in single quotes; use backticks for identifiers if needed.
  • Temporarily turn on error reporting while debugging so you see warnings, then disable or log them once fixed so they do not corrupt AJAX output.
  • For the dependent cemetery box, pass a town id (not just the town name) to the second endpoint to avoid ambiguity.

A compact, robust server-side pattern (use PDO and return JSON):

<?php
// endpoint: towns.php?q=prefix
$pdo = new PDO('mysql:host=HOST;dbname=DB;charset=utf8mb4','USER','PASS',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$stmt = $pdo->prepare("SELECT DISTINCT Town FROM towns WHERE Town LIKE :q ORDER BY Town LIMIT 15");
$stmt->execute([':q' => (isset($_GET['q']) ? $_GET['q'] . '%' : '%')]);
$names = $stmt->fetchAll(PDO::FETCH_COLUMN);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($names);

Final notes: index the Town column for speed, use LIMIT to avoid huge payloads, prefer IDs for cascading lookups, and always use prepared statements to prevent SQL injection. If the demo you used expects newline-separated text instead of JSON, return implode("\n", $names) and text/plain instead of JSON. Thanks to for prompting the narrowing-down approach.

Recommended Answers

All 6 Replies

The script above performs a query but doesn't output anything. You will need an output from the script to populate the suggestions. What does the demo script look like?

Hi, Thanks for your reply. I have attached the three files that the demo uses. I am fairly new to javascript so I haven't messed with that. Again when I test the php page it showed up with the correct info.

buddylee17, Have you by any chance made any progress? Do you have any ideas on how to get it to work? I would appreciate the help.

Does anyone have any suggestions on how to get this to work. I would appreciate the help. I, myself, am stumped.

Narrow down the problem and post the code. Most of us aren't going to download your code.

ok guys, I think I have solved the problem. It was a simply a mis-reference. The script called on the town ID where I accidently renamed that to Town. I don't know if you guys understand what I was trying to say, bt anyway, I've got it solved. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.