2013-03-26 72 views
0

我正在使用select2插件從mysql填充標籤。 所以我傳遞將q變量的getdada.php.but問題是爲什麼變量不被傳遞?

  1. q變量沒有被傳遞訪問getdata.php。我想通過這個變量,這樣我可以得到有關它的數據(可能是我沒有把q變量放在合適的地方)

    2.if我不使用q變量,那麼只有最後或第一個變量retrived.I想填充所有的結果作爲標籤數據(也許錯誤是在我的jsoncode在getdata.php甲酸鹽)

Plz的幫助。

 $("#e8").select2({ 


     placeholder: "Search for another Concept", 
     minimumInputLength: 1, 
     multiple: true, 
     ajax: { // instead of writing the function to execute the request we use Select2's convenient helper 
      url: "getdata.php", 
      dataType: 'json', 
      data: function (term, page) { 
       return { 
        q: term, // search term 

         page: page 

       }; 

      }, 
      results: function (data, page) { 
       return { results: data}; 

      } 
     } 
    }); 

這裏訪問getdata.php

$sql=mysqli_query($db3,"SELECT * FROM o4_tags"); 

while($row=mysqli_fetch_array($sql)){ 
    $tags=$row['tag_name']; 
    $id=$row['id']; 

     $myArray = array(
      array("id" => "$id", "text" => "$tags"), 
     ); 


} 

echo json_encode($myArray); 
+0

以防萬一有人需要這個:這裏是Select2他在談論http://ivaynberg.github.com/select2/ – Hazzit 2013-03-26 23:23:11

回答

1

一開始,似乎有一些與你的PHP腳本去錯了。您正在爲檢索的每一行創建一個新的$ myArray。只有這些數組中的最後一個實際上以JSON形式輸出。此代碼解決了問題:

$sql=mysqli_query($db3,"SELECT * FROM o4_tags"); 
$myArray=array(); 

while($row=mysqli_fetch_array($sql)){ 
    $tags=$row['tag_name']; 
    $id=$row['id']; 

    $myArray[]=array("id" => "$id", "text" => "$tags"); 
} 
echo json_encode($myArray); 

我不知道爲什麼你沒有看到在你的PHP腳本的檢索詞,不過話又說回來,我沒有看到任何PHP代碼實際上與交易搜索詞。您是否嘗試過傾銷$ _REQUEST?

+0

非常感謝。我實際上通過使用$ q = $ _GET [「q 「]; – 2013-03-26 23:36:32