doudihuang7642 2014-06-10 15:24
浏览 105
已采纳

PHP + Ajax为实时搜索传递输入关键字

I already asked this, but I guess it was too abstract and all I got was suggestions to use DataTables plugin, however I want to do this live search manually.

I would like to know how can I pass a keyword (and how to create it for sending )from the input box to the findUser(); function with MySQL search query on every .keyup and refresh the shown content in #theContent.

Right now it correctly finds the people with ".com" because in users.ajax.php findUser('.com'); function has ".com" keyword manually written in instead of using the input box.

Currently, index.php:

<input id="search">
    <table border="1" BORDERCOLOR=black>
    <thead> 
    <tr>
        <th>Name</th><th>LastName</th><th>E-Mail</th>
    </tr>
    </thead>

    <tbody id="theContent">

    </tbody>

    </table>

    </body>

    <script type="text/javascript">

    function loadUser(){

        $.ajax({
            url: 'users.ajax.php'

        }).done(function(data){
            var HTML = '';
            data = JSON.parse(data);

            $.each(data['usersData'], function(key, val){
                HTML += getSingleUserLine(val);
            });


            $('#theContent').html(HTML);

            $( '#search' ).keyup(function() {   // need it to send the keyword here and refresh the results?
                 alert( "Handler for .keyup() called." );
            });

        });
        }

    function getSingleUserLine(data){
        if(data){
            var string = '';

            string = '<tr><td>'+data.fname+'</td><td>'+data.lname+'</td><td>'+data.email+'</td></tr>';

            return string;

        }else{
            return false;
        }
    }

    $(document).ready(function(){
            loadUser();
    });
    </script>

in users.ajax.php:

$return = array();

    include('db.class.php');
    include('user.class.php');

    $DB = new DB(XXXXXXXX)
    $USER = new User(); 

    $data = $USER->findUser('.com');


    $return['usersData'] = $data;

    echo json_encode($return);
  • 写回答

3条回答 默认 最新

  • ds355020 2014-06-10 16:24
    关注

    I decided to start from the ground up, and found quite an easy solution:

    I edited the loadUser() like so :

    function loadUser(){
    
            $.ajax({
                url: 'users.ajax.php',
                data: { 'keyword': $('#search').val() },
    
    
            }).done(function(data){
                var HTML = '';
                data = JSON.parse(data);
    
                $.each(data['usersData'], function(key, val){
                    HTML += getSingleUserLine(val);
                });
    
                $('#theContent').html(HTML);
    
            });
    
        }
    

    then in the end:

    $(document).ready(function(){
    
    
                $( '#search' ).keyup(function() {
                     loadUser();
                });
    
        });
    

    users.ajax.php:

    $data = $USER->findUser($_GET['keyword']);
    

    Thanks for Your time trying to help me out though!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)