douna1895 2015-10-06 17:40
浏览 26
已采纳

带有ajax和查询的实时搜索引导程序

Me and some mates have been trying to make a live search script on our search bar. Now this isn't going that well so now we are asking for ur help!

our external file to get the results is this :

$con = mysqli_connect('localhost', '*', '*', '*');

$key=$_POST['search'];

$query = ("select name, url from search where name LIKE '%{$key}%'");
$sql = $con->query($query);

while($row = $sql->fetch_array()){
    echo json_encode($row);
}

and our script code looks like this :

        <script>

    $(document).ready(function(){

$( "#formGroupInputLarge" ).keyup(function() {
  console.log( "Handler for .keyup() called." )
        var string = $('#formGroupInputLarge').val();
            $.ajax(
                {
                    type: 'POST',
                    url: 'search.php',
                    data: {'search': string},
                    success: function(data){
                        var text= JSON.parse(data);
            $("#suggesstion-box").show();
            $("#suggesstion-box").html("<a href='#'>"+ text +"</a>");
            $("#search-box").css("background","#FFF");
        }
                }
            );
        }); 

    });
    </script>

we've tried with multiple things like the next ones :

<script>

$(document).ready(function () {

    $("#formGroupInputLarge").keyup(function () {
        console.log("Handler for .keyup() called.");
        var string = $('#formGroupInputLarge').val();
        $.ajax(
            {
                type: 'POST',
                url: 'search.php',
                data: {'search': string},
                success: function (data) {

                    var obj = eval('('+ data +')' );
                    console.log(obj['name']);
                    //var text = JSON.parse(data);
                    //$("#suggesstion-box").show();
                    //$("#suggesstion-box").html(text);
                    //$("#search-box").css("background", "#FFF");
                }
            }
        );
    });

});

</script>

but none of it seems to work. Please help us!

  • 写回答

1条回答 默认 最新

  • doujieyu7062 2015-10-06 18:03
    关注

    So many things could be wrong in your code ! You need to give more info on how each part behaves (error messages, etc).

    Check this to get a good look at how everything work with AJAX, and nice and simple examples http://www.w3schools.com/php/php_ajax_intro.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀