【问题标题】:How to search using ajax in codeigniter?如何在codeigniter中使用ajax进行搜索?
【发布时间】:2018-01-18 11:36:59
【问题描述】:

控制器 (invoice_invoice.php)

<?php

function sent() {
    if (!null($this->input->get('id'))) {
        $limit_per_page = 10;
        $page = ($this->uri->segment(3)) ? ($this->uri->segment(3) - 1) : 0;
        $total_records = $this->invoice_model->get_total_search($this->input->get('id'));
        if ($total_records > 0) {
            $params = array();
            $params['content'] = $this->invoice_model->search($this->input->get('id'), $limit_per_page, $page * $limit_per_page);

            //print_r($data);
            //exit;
            //$abc =  count($data['content']);
            $config['base_url'] = site_url() . '/invoice_invoice/sent';
            $config['total_rows'] = $total_records;
            $config['per_page'] = $limit_per_page;
            $config["uri_segment"] = 3;

            $config['num_links'] = 2;
            $config['use_page_numbers'] = TRUE;
            $config['reuse_query_string'] = TRUE;

            $config['full_tag_open'] = '<div class="pagination dataTables_paginate paging_bootstrap_number" id="sample_3_paginate"> <ul class = "pagination" style = "visibility : visible;">';
            $config['full_tag_close'] = '</ul></div>';

            /*  $config['first_link'] = 'First Page';
              $config['first_tag_open'] = '<span class="firstlink">';
              $config['first_tag_close'] = '</span>'; */

            //$config['last_link'] = 'Last Page';
            //$config['last_tag_open'] = '<span class="lastlink">';
            //$config['last_tag_close'] = '</span>';

            $config['next_link'] = '<i class="fa fa-angle-right"></i>';
            $config['next_tag_open'] = '<li class = "next">';
            $config['next_tag_close'] = '</li>';

            $config['prev_link'] = '<i class="fa fa-angle-left"></i>';
            $config['prev_tag_open'] = '<li class = "prev">';
            $config['prev_tag_close'] = '</li>';

            $config['cur_tag_open'] = '<li class = "active"><a>';
            $config['cur_tag_close'] = '</a></li>';

            $config['num_tag_open'] = '<li>';
            $config['num_tag_close'] = '<li>';

            $this->pagination->initialize($config);

            // build paging links
            $params["links"] = $this->pagination->create_links();
        }
    } 
}

我的控制器正在从 jquery 中调用 id 来运行它。

这是我的 jquery

$(document).ready(function () {
    $("#search").keyup(function () {
        var str = $("#search").val();
        if (str != "") {
            console.log(str);
            $.get("<?php echo base_url();?>invoice_invoice/sent?id=" + str, function (data) {
                $("#sample_1").html(data);
            });
        }
    });
});

sample_1 是我要打印来自搜索的值的表的 id

这是带有搜索块的视图

<div class="row">
<div class="col-md-6 col-sm-6"><div 
class="dataTables_length" id="sample_2_length">
<label>Show <select 
name="sample_2_length" aria-controls="sample_2" class="form-control input-sm 
input-xsmall input-inline">
<option value="5">5</option>
<option value="15">15</option>
<option value="20">20</option><option value="-1">All</option>
</select>
</label>
 </div>
</div>
<div class="col-md-6 col-sm-6"><div id="sample_2_filter" 
class="dataTables_filter">
<label>Search:
<input type="search" id ='search' name = 'search' class="form-control input-
sm input-small input-inline" placeholder="" aria-controls="sample_2">
</label>
</div>
</div>
</div>

这里是模型函数

function search($search,$limit,$start)
{
    $userdata1 = $this->session->userdata('condition');
    $this->db->select("'id,Client_Name,ac_email,Client_email,Invoice_no,Items,client_add,Currency,Language,Notes'");
    $whereCondition = array('Client_email' =>$search , 'ac_email' => $userdata1['ac_email'] );
    $this->db->where($whereCondition);
    $this->db->from('invoices');
    $params = $this->db->get();
    return $params->result();
}

我只想在数据库中搜索我的数据并将其打印到视图中。我已经创建了视图并完成了分页,但无法使搜索顺利进行。我只是在运行搜索视图的控制台中收到“GET http://localhost/cidemo/invoice_invoice/sent?id=weqe@dsf404(未找到)”错误

【问题讨论】:

  • 网址http://localhost/cidemo/invoice_invoice/sent?id=weqe@dsf如果您只是浏览它真的有效吗?
  • 最好不要分页显示搜索结果。在这种情况下,分页按钮将不起作用,因为在没有查询字符串的情况下初始化了`$config['base_url']`。您可以使用文本框的自动完成功能来搜索项目。

标签: jquery ajax codeigniter search


【解决方案1】:

在java中

$.ajax({
    type: "post",
    url: url,
    data: formData,
    processData: false,
    contentType: false,
    success: function (data) {
        console.log(JSON.stringify(data.revalformbean));
        //console.log(JSON.stringify(data.examformbean.full_name));
        $("input[type='hidden'][name='revalformbean.exam_detail_id']").val(data.revalformbean.exam_detail_id);
        //$("input[type='hidden'][name='examformbean.full_name']").val(data.examformbean.full_name);
        $("input[name='revalformbean.name']").val(data.revalformbean.first_name);
        $("input[name='revalformbean.month_year']").val(data.revalformbean.exam_month_year);
        $("input[name='revalformbean.seat_no']").val(data.revalformbean.seat_no);

        var errorMsg = data.errorMsg;
        //console.log("form "+errorMsg);
        //json2array(data.customResult);
        if (errorMsg != null) {
            //alert(errorMsg);
            $('#errorField').text(data.errorMsg);
            $('#' + formId).find('input[type="submit"]').removeAttr('disabled');
        } else {
            fieldSetHide(formId);
        }
    }
});

【讨论】:

    【解决方案2】:

    invoice_invoice 控制器的 sent 方法中

    你正在设置:

    $config['base_url'] = site_url() . '/invoice_invoice/sent';
    

    这使您的基本 URL:http://localhost/cidemo/invoice_invoice/sent

    然后在你的 JS 中这样做:

    P.S: 我假设你的 JS 函数是在 .php 文件中定义的 封装在 &lt;script&gt; 标记中。

    $.get("<?php echo base_url();?>invoice_invoice/sent?id=" + str, function (data) {
        $("#sample_1").html(data);
    });
    

    这反过来使您的 get 函数看起来像:

    $.get("http://localhost/cidemo/invoice_invoice/sent/invoice_invoice/sent?id=" + str, function (data) {
        $("#sample_1").html(data);
    });
    // OR http://localhost/cidemo/invoice_invoice/sentinvoice_invoice/sent    
    // if taking on account the **absence** of trailing slash
    

    两次查看控制器及其方法的定义,因此404

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多