【问题标题】:search database with ajax and codeIgniter使用 ajax 和 codeIgniter 搜索数据库
【发布时间】:2014-09-06 14:33:29
【问题描述】:

我想进行搜索,但我得到重复的结果,如何从 prev ajax 请求中删除 prev 代​​码我在 ajax 中的代码是:

<script>
$(function () {


    $("#sample_search").keyup(function () {
        var that = this,
        value = $(this).val();


            $.ajax({
                type: "post",
                url: "<?php echo site_url('home/search')?>",
                data: {
                    'search_keyword' : value
                },
                dataType: "text",
                success: function(msg){
                    //we need to check if the value is the same
                    $("#prostor").append("<p id='rezultati'>"+msg+"</p>");
                }
            });

    });
}); 


</script>

在php中

 public function search(){
        if(isset($_post['search_keyword'])){

            $var = $_post['search_keyword'];
            $q = $this->db->query("SELECT * from users where username like '%{$var}%'");
            foreach ($q ->result() as $a){
                echo $a->username;
            }

        }
    }

我的问题是如何删除上一个搜索结果

【问题讨论】:

  • “重复代码”你的意思是什么?请指定您需要的输出和当前输出。
  • 我的意思是repatavive结果,例如如果我输入b然后我,我会得到billbill的结果
  • $_post['search_keyword'](small $_post) 为你工作吗?
  • 为了防止结果重复,请在您的 jquery 代码中使用 html() 而不是 append()

标签: jquery ajax codeigniter search


【解决方案1】:

$.ajax 调用中使用html() 而不是append(),如下所示

success: function(msg){
 //we need to check if the value is the same
 $("#prostor p#rezultati").html(msg);
}

【讨论】:

    【解决方案2】:

    你有问题在这里:

    $("#prostor").append("<p id='rezultati'>"+msg+"</p>");
    

    问题:您反复将具有相同id 但内容不同的段落附加到prostor。从一开始就在prostor 中包含&lt;p&gt; 标签就足够了,然后始终使用.text()/.html() 函数更改其text/html

    【讨论】:

    • tnx 正确,我给上层的人奖金,因为他第一次回复
    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多