2011-04-28 5 views
1

좋아, 나는 codeigniter와 함께 자동 완성을 사용하려고합니다. 정규 HTML, JQuery 및 PHP를 사용하여이 정확한 방법을 수행했습니다. 내가 codeigniter와 함께 작동하도록 비트를 수정하려고했지만 작동하지 않습니다.코드 서명자로 자동 완성 JQuery ui

JQuery와

$("#update-text").autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>",dataType:"json"}); 

USERPROFILE 제어기로 기능 자동 완성

function autocomplete(){ 
    // this takes the text field and whatever the user writes it autocompletes it. 
    //Every single place and event in the database should be displayed in this view in this format 



    $this->load->view("source", $data); 

    } 

PHP 파일

<form method="post" action="#" name="updatePlanForm"> 
<div class="ui-widget"> 
<label for="update-text"></label> 
<input type="text" id="update-text" name="updateText" value="What are you gonna do today?" onclick="removeText()"/> 
</div> 
<input type="button" class="small green button" value="Update Plan" name="updatePlanButton"/> <!-- once clicked JQuery sends a post to a controller send_plan and jquery will return the view --> 
</form> 

마지막 소스 PHP 파일 형태

<?php 

$req = $_GET['term']; //first get the search keyword as get method 

$arrResults = array('orange', 'apple', 'bannana'); 

$array = array_filter($arrResults, 'mycallback'); 
//filter the array containing search word using call back function 

function mycallback($var) 
{ 
    global $req; 
    if(preg_match('/^'.$req.'/', $var)) 
    {  
     return $var; 
    } 
} 

$array1 = array(); 

//filter null array 
foreach($array as $arr => $val) 
{ 
     if(!empty($val)) 
     { 
       $array1[] = $val; 
     } 

} 

//echo out the json encoded array 
echo json_encode($array1); 

?> 

답변

1

당신은 정말로 당신의 견해와 같은 논리를 가져서는 안됩니다. 또한 $ _GET [] 변수는 컨트롤러에서 뷰를로드 할 때 데이터로 채워지지 않습니다. 실제로 $ _GET []는 CI에서 쿼리 문자열이 기본적으로 꺼져 있기 때문에 전혀 작동하지 않습니다. 당신은 그들을 켤 수 있지만,이 경우에는 필요가 없습니다.

우선과 같이, 컨트롤러에 직접 자동 제안 PHP 코드를 넣어 : 다음과 같이 더 적합한 솔루션을 구현할 수 GET

function autocomplete() { 
    $req = $this->input->post('term'); 

    $arrResults = array('orange', 'apple', 'bannana'); 

    $array = array_filter($arrResults, 'mycallback'); 
    // filter the array containing search word using call back function 

    function mycallback ($var) { 
    global $req; 

    if (preg_match('/^'.$req.'/', $var)) { 
     return $var; 
    } 
    } 

    $array1 = array(); 

    // filter null array 
    foreach ($array as $arr => $val) { 
    if(!empty($val)) { 
     $array1[] = $val; 
    } 
    } 

    //echo out the json encoded array 
    echo json_encode($array1); 
} 

그리고 대신의 POST를 사용하도록 jQuery를 호출을 변경

$('#update-text').autocomplete({source:"<?php echo site_url('userProfile/autocomplete');?>", dataType:'json', type:'POST'}); 

검색을 구현하는 더 좋은 방법이 있지만 올바른 길을 찾아야합니다. 데이터베이스에 연결하면 결국 '용어'에 대한 간단한 LIKE 쿼리가 제대로 작동합니다.

+0

고맙습니다, 일리노이 테스트 이렇게 받아들입니다 :) – Kay

+0

나는 그것을 시도 .. 작동하지 않았다. 어쩌면 get 메소드를 사용해야합니까? – Kay

+0

늦게 답장을 드려 죄송합니다. 그것의 어떤 부분이 작동하지 않았습니까? 포스트그 요청/응답을 추적하고 무슨 일이 일어 났는지 알려주려면 방화 녀를 사용하십시오. GET을 사용하려면 CI 구성에서 쿼리 문자열을 활성화해야하지만 GET과 POST의 차이는 문제의 원인이되지 않습니다. –

0

이것이 매우 늦었지만 CodeIgniter에 대한 jQuery UI 자동 완성을 수정하는 쉬운 방법은 JS 파일의 일부분을 자동 완성하여 GET 대신 POST로 아약스 유형을 설정하십시오.

jQuery UI .js 파일에서 "autocomplete"섹션을 검색하면이 코드 블록 내에서 유일한 ajax 호출 (하나만 있어야 함) 만 찾습니다. 매개 변수에 다음을 추가하십시오.

이렇게하면 GET 대신 POST를 사용하도록 자동 완성이 변경됩니다.