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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料