php ajax json response html output wont work

问题内容:

I need some help getting html output from json data. I am trying to make a facebook style search friend system using AJAX,PHP(JSON) .

All php and ajax code is working fine. I just need to know how can i show all result in a div.

If you want to search Rihanne from facebook you should write rihanna name from facebook searchbox. When you start typing like R then you will see other names starting with R. So my problem is here , how can i show all other names in the html output.

At this point, the codes are just the final result. Like When i write Rihanna then i get output of Rihanna name.

HTML

    <input type="text" id="searchkey" class="searchBox" placeholder="Search"/>
    <div class="new-page-form-container" id="np">
           <div class="un"></div> 
    </div>

AJAX

$('body').delegate('#searchkey','keyup', function(){

          var box = $('#searchkey').val();
          contentbox = $.trim(box);
          var dataString = 'keys=' + contentbox;
          if(contentbox !==''){
            $.ajax({
              type: "POST",
              url: siteurl +"requests/src",
              data: dataString,
              dataType:"json",
              cache: false,
              beforeSend: function(){},
              success: function(data){ 
                 $.each(data, function(key, value) {
                     //console.log(value.username + ": " + value.fullname); 
                     $("#np").append("Username: " + value.username + "<br/>");  
                 }); 
              }
            });
          } 
   }); 

src.php

$keys = mysqli_real_escape_string($db, $_POST['keys']);
     // Check the word from data for username
     $keyRestuls = $HuHu->SearchUser($keys);
     if($keyRestuls) {
        // If array is in data
     foreach($keyRestuls as $datas) { 
        $dataUsername = $datas['username'];
        $dataUserID = $datas['fullname'];
        $data_json[] = array(
          'username' => $dataUsername,
          'fullname' => $dataUserID
        );  
       echo json_encode( $data_json );  
 }
}

问题评论:

    
It’s not clear where you think the problem is. Is an ajax request being made for each keystroke? If so, is the expected response being returned for each? If so, can you show an example of the HTML that is being generated by the response and show how this differs from what you want?
    
@PatrickQ Is an ajax request being made for each keystroke? (YES) If so, is the expected response being returned for each? (YES)
– DevStud
43 mins ago
    
@PatrickQ If so, can you show an example of the HTML that is being generated by the response and show how this differs from what you want? (I am asking this)
– DevStud
42 mins ago
    
Works for me: jsfiddle.net/uq8pu3Lz

原文地址:

https://stackoverflow.com/questions/47755918/php-ajax-json-response-html-output-wont-work

添加评论