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条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿