0

hackre、christian.thomas、olimortimer の助けを借りて回答を更新しました。

問題: フィルタリングしていませんでした。

だから私はそれをどのように解決しましたか:

Jクエリ:

$(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 コンポーネントであり、トークンを送信する「受け入れられた」方法で Google 検索を行う必要がありました。

次に、それが私の 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);
    }

再びありがとう!

元の投稿

私はオートコンプリートを設定しようとしていますが、ほとんどの場合はうまくいきましたが、残りのいくつかの問題を微調整しようとしています.

現在気になっているのは、たとえば「Scholarship」(私の DB にある奨学金の偽名) と入力すると、「fewfew」(別の名前) も表示されることです。

私が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);        
    }

それが役立つ場合、CSRFをオンにしてCodeigniterも使用していますが、使用しているフォームが 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>

御時間ありがとうございます。

編集 :

私が得ている応答の写真

ここに画像の説明を入力

4

1 に答える 1