dongwei1954 2016-06-07 08:21
浏览 64
已采纳

jQuery PHP Mysql jSon显示循环数据

I have jQuery Ajax Autosuggest using jSon.

Now I have problem when showing the data. The data get from mysql data using PHP (looping data) but when get the result, it always show 1 row.

Here is my js code:

$.ajax(
{
    type: "GET",
    data: post_string,
    dataType: "json",
    cache: false,
    url: 'search.php',
    success: function(data)
    {
        full_name = data[0].full_name;
        username = data[0].username;

        $("#divResult").show();
        $(".display_box").html(username);
    }
});

and the search.php

$getSearchWord = mysqli_real_escape_string($con, $_GET['searchword']);
$json = array();

$searchQuery = mysqli_query($con, "SELECT * FROM tb_users WHERE username LIKE '%$getSearchWord%' OR full_name LIKE '%$getSearchWord%' LIMIT 5");
while($searchFetchData = mysqli_fetch_array($searchQuery))
        {
$json[] = array(   
            'username' => $searchFetchData['username'],
            'full_name' => $searchFetchData['full_name']
            );
}

echo json_encode($json);

and html div to display

<div id="divResult">
    <div class="display_box"></div>
</div>
  • 写回答

3条回答 默认 最新

  • doufu1950 2016-06-07 08:32
    关注

    Json

    To clear out the field, call this before the Ajax request:

    $("#divResul").hide(200);
    $(".display_box").html('');
    

    You can try to run all returned array before putting it in your .display_box. Get the length of array returned from search.php then run it in a loop.

    success: function(data){
    
        $("#divResult").show(200);
    
        var n = data.length;
    
        for(var x = 0; x < n; x++){
    
            $(".display_box").append(data[x].full_name);
            $(".display_box").append(data[x].username);
    
        }
    
    }
    

    No Json

    OR without using json. From your search.php:

    $table = '<table>';
    
    $getSearchWord = mysqli_real_escape_string($con, $_GET['searchword']);
    $json = array();
    
    $searchQuery = mysqli_query($con, "SELECT * FROM tb_users WHERE username LIKE '%$getSearchWord%' OR full_name LIKE '%$getSearchWord%' LIMIT 5");
    while($searchFetchData = mysqli_fetch_array($searchQuery))
    
        $table .= '<tr>
                       <td>'.$searchFetchData['username'].'</td>
                       <td>'.$searchFetchData['full_name'].'</td>
                   </tr>';
    }
    
    $table .= '</table>';
    
    echo $table; /* RETURN THIS TO YOUR AJAX REQUEST */
    

    Then on your Ajax request:

    $.ajax(
    {
        type: "GET",
        data: post_string,
        url: 'search.php',
        success: function(data)
        {
            $("#divResult").show(200);
            $(".display_box").html(data);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?