0

Right now I have a site that has a dropdown menu, and in that menu contains about 1200 locations of different places. What I want to do is replace that dropdown menu with a textbox and when the user starts typing in the name of the location, it will populate the list accordingly. I have been able to do this successfully using an array that I create in the actual script block of my code, but what I want to do is not replace that with the data that comes from a MySQL query from an external php file. This is what I have working using the array.

Here is the php associated with this question:

<?php
include('common.php');
$sql = "SELECT LoginUserName FROM LoginUser";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
    $users = $row['LoginUserName'];
//echo $hour.":".$minutes;
}
// $timeHour = $_POST['hours']; This isn't needed.
echo $users;
$content = $_POST['users']; //get posted data
?>

Here is what I currently have working using the array hard coded

<script>
  $(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ];

    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

And then the html used on the page is

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>

So now what I am trying to do is to to use AJAX to call the php script which has the MySQL query and return what it gets and in turn populate the search results to have the same effect. Here is what I have come up with, keep in mind I am still new to the jQuery / AJAX thing...

<script>
  $(function() {
    var availableTags = [
      $.ajax({
       type: "POST",
       url: "getme.php",
       data: { content: content
       }
      })
    ];

    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>

Any help is greatly appreciated. Thank you very much in advance

4

1 回答 1

1

添加一个javascript函数

$(function() {
   //autocomplete
   $("#tags").autocomplete({
    source: "search.php",
        minLength: 1
   });              
});

在 search.php 中

$query = "SELECT tags FROM YOUR_TABLE WHERE tags like '%".$_GET['tags']."%' LIMIT 0,15";
$rs = mysql_query($query);
while ($row = mysql_fetch_array($rs)) {
    $tags[] = $row['tags'];
}

echo json_encode($tags);

请使用限制来加快自动建议

于 2013-09-16T18:12:09.500 回答