weixin_33686714 2016-02-10 19:01 采纳率: 0%
浏览 5

Ajax发布缓慢

I have a page with from, and I maked an input, and when the user is writing, it's automaticlly will show the result from the DB(LIKE SEARCH). I used jquery. It's working but it's runing very slow. Can antbody help me?

The code is:

<html>  
    <head>
        <title></title>
        <link rel='stylesheet' type='text/css' href='new_style.css' />
          <script src="jquery.js"></script>

          <script type='text/javascript'>
              setInterval(function() { 
                  $('.input').keyup(function(){
                      var a = $('.input').val();
                      $.post('select.php?id=28',{"name":a}, function(data){
                          $('#msg').html(data);
                      });
                  });
              },1000);
         </script>
    </head>
    <body>
<?php
if(isset($_GET['id'])){
    if(isset($_POST['name'])){
        $name = $_POST['name'];
        if($_POST['name']==""){
            echo "text";
        } else {
            $query = mysqli_query($user->getDB(),"SELECT * FROM bla WHERE name  LIKE '%$name%'");
            if(mysqli_num_rows($query)==0){
                echo "text";
            } else { ?>
        <table id='tables'>
            <tr>
                <th>name</th>
                <th>text</th>
                <th>text</th>
                <th>text</th>
            </tr><?php
                while($show =mysqli_fetch_assoc($query)){
                    echo $show['name'];
                }
            }
            echo "</table>";
            if(isset($_POST['check'])) {
                foreach($_POST['ch'] as $selected) {
                    echo $selected . " <br />";
                }
            }
        }
    } else { ?>
        <p id='msg'></p>
    </body>
</html>
<?php
    }
}
?>
  • 写回答

4条回答 默认 最新

  • weixin_33712881 2016-02-10 19:22
    关注

    for performance, try this

    <script type='text/javascript'>
        $(function(){
            var pendingRequest;
            $('.input').keyup(function(){
                if(pendingRequest){
                    pendingRequest.abort()
                    pendingRequest = null;
                }
    
                var a = $('.input').val();
                pendingRequest = $.post('select.php?id=28',{"name":a}, function(data){
                    pendingRequest = null;
                    $('#msg').html(data);
                });
            });
        })
    </script>
    

    then fix the vulnerability, MarcB mentionied in the comments.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏