簡體   English   中英

Jquery UI自動完成 - CodeIgniter - CSRF +模型+控制器

[英]Jquery UI Auto Complete - CodeIgniter - CSRF + Models + Controllers

在hackre,christian.thomas,olimortimer的幫助下更新了答案:

問題:我沒有過濾。

所以我如何解決它:

Jquery:

$(function() {
  var cct = $.cookie('ccn');
    $( "#searchscholarship" ).autocomplete({
      source: function(request, response) {
        $.ajax({
          url: '/global/c_ajax/ajax_scholarshipNames',
          type: "post",
          data: {term: request.term, 'ctn': cct},
          dataType: "json",
          success: function(data) {
            response( $.map( data.myData, function( item ) {
              return {
                label: item.scholName,
                value: item.scholID
              }
            }));
          }
        });
      },
      minLength: 3,
    });
  });

AJAX和CI的大問題是CSRF組件,不得不對發送令牌的“接受”方法進行谷歌搜索。

然后發送到我的c_ajax控制器:

public function ajax_scholarshipNames() 
    {
        $this -> load -> model('m_ajax');
        $post = $this -> input -> post();
        $data = $this -> m_ajax -> scholarships($post);
        foreach ($data as $d) {
            $value['myData'][] = array(
                'scholID' => $d["intScholarshipID"],
                'scholName' => $d["txtScholarshipName"]
            );
        }
        $this -> output -> set_header('Content-Type: application/json; charset=utf-8');
        echo json_encode($value);
    }

然后在模型中:

public function scholarships($post) // I usually don't use model just for database transactions but in this use case thats all I need the model to do. 
    {
        $name = '%' . $post["term"] . '%';
        $sql = "SELECT * FROM tableScholarship WHERE txtScholarshipName LIKE :name";
        $ajax_schol = $this -> db -> conn_id -> prepare($sql);
        $ajax_schol -> bindParam(":name", $name);
        $ajax_schol -> execute();

        return $ajax_schol -> fetchAll(PDO::FETCH_ASSOC);
    }

再次謝謝你!

原始郵政

我正在嘗試設置自動完成,並且在大多數情況下我已經做得很好,除了現在我正在嘗試用它來調整剩余的幾個問題。

目前困擾我的是,當我輸入例如“獎學金”(這是我的數據庫中的獎學金的假名)時,也會出現“少數幾個”(這是另一個名字)

如果我單獨輸入S獎學金應該是唯一不顯示“少數幾個”的東西

我的Jquery:

$(function() { 
    $( "#searchscholarship" ).autocomplete({
      source: function(request, response) {
        $.ajax({
          url: '/global/c_ajax_controller/ajax_scholarshipNames',
          data: {term: request.term},
          dataType: "json",
          success: function(data) {
            response( $.map( data.myData, function( item ) {
              return {
                label: item.scholName, // Here it will either be Scholarship or fewfew
                value: item.scholID // 8 or 9
              }
            }));
          }
        });
      },
      minLength: 1,
    });
  });

我在ajax控制器中的Php函數:

public function ajax_scholarshipNames() 
    {
        $data = $this -> m_global -> db_data($where = array(), $table = "tableScholarship");

        foreach ($data as $d) {
            $value['myData'][] = array(
                'scholID' => $d["intScholarshipID"],
                'scholName' => $d["txtScholarshipName"]
            );
        }
        $this -> output -> set_header('Content-Type: application/json; charset=utf-8');
        echo json_encode($value);        
    }

如果它有幫助我也使用Codeigniter並且CSRF打開但是它似乎沒有影響結果,因為我使用的表單是使用form_open:

<div id="editscholarship">
  <?php
    if (!$this -> session -> userdata('scholarshipID')) {
        echo form_open('/global/c_scholarshipmaintance/editscholarship/' . urlencode('1'));
        echo 'Please Enter Scholarship Name Below : <br>' . form_input('findScholarship', '', 'autocomplete="off", id="searchscholarship"');
    }
  ?>
</div>

謝謝你的時間。

編輯:

我得到的一張照片

在此輸入圖像描述

我不認為你正在過濾返回的值,因為你缺少jQuery Ajax函數中的選項,它告訴它要過濾什么。

請參閱此處的示例 - http://jqueryui.com/autocomplete/#remote-jsonp

      data: {
        featureClass: "P",
        style: "full",
        maxRows: 12,
        name_startsWith: request.term
      },

但是,我更喜歡將查詢發送到PHP控制器,並在對數據庫運行時過濾值。 這樣,您只需發回所需的值,而不是所有值,然后進行過濾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM