weixin_33712987 2015-01-08 20:29 采纳率: 0%
浏览 29

PHP AJAX JSON自动建议

I cannot get the dropdown field to "autosuggest".

I have my main page, which is bpSearch.php, where I have a modal window with an input field that, when you begin typing, it should autosuggest data that is being called from my search file.

Here is the input inside the modal:

 <input type="text" class="autosuggest" id="partnerName" name="partnerName" placeholder="Partner Name" />
 <div class="dropdown">
   <ul class="result"></ul>
 </div>

Here is my javascript, called searchPartner.js, that is sending the RESULT to the search file:

 $(document).ready(function()
 {
   $('.autosuggest').keyup(function()
   {
     var search_term = $(this).attr('value');
     $.post('api/searchPartner.php', {search_term:search_term}, function(data)
     {
       $('.result').html(data);
       $('.result li').click(function()
       {
         var result_value = $(this).text();
         $('.autosuggest').attr('value', result_value);
       });
     });
   });
 });

And here is my search file, called searchPartner.php. As you can see in the JavaScript, it is located in a folder called api. Perhaps I'm not traversing to the directory correctly? Here is searchPartner.php:

 <?php
   include("../include/database.php");
   if(isset($_POST['search_term']) == true && empty($_POST['search_term']) == false)
   {
     $search_term = mysqli_real_escape_string($_POST['search_term']);
     $search = "SELECT PARTNER_CODE, FULL_NAME FROM partner WHERE FULL_NAME LIKE '$search_term%'";
     $query = mysqli_query($dbc, $search);
     while(($row = mysqli_fetch_assoc($query)) !== false)
     {
       echo '<li>', $row['FULL_NAME'], '</li>';
     }
   }
 ?>

I know this query works, because I've opened the page, commented out the IF statement, and I can see the LI tags with the $row{'FULL_NAME']. So all I need to do is get my main page to retrieve this data when the user begins typing.

  • 写回答

1条回答 默认 最新

  • weixin_33720956 2015-01-08 22:10
    关注

    I think you might want to try

    // var search_term = $(this).attr('value');
    var search_term_2 = $(this).val();
    $.post('api/searchPartner.php', {search_term:search_term_2}, function(data)
    // ...
    

    Sorry the search_term:search_term was just confusing me... But rather than looking at the attr value , just get the val()

     <input type="text" class="autosuggest" id="partnerName" name="partnerName" placeholder="Partner Name" value="No Values Here Man..." />
    

    not sure if this'll help though

    评论

报告相同问题?