2010-12-01 81 views
0

這是earlier post的延續。不幸的是,所發佈的解決方案無效,我的後續問題未得到解決。以防萬一這是因爲我的慷慨受訪者沒有注意到我回復了,我正在重新發布我的問題。如何使用PHP將數據提供給jQuery自動完成?


我試圖建立一個表單,其中某些文本字段和文本區域有自動完成。

我已經使用WordPress的強大的插件來建立我的表單。我正在使用jQuery autocomplete plugin作爲自動完成部分。

貫徹我的受訪者的意見後,現在該代碼看起來是這樣的:

<script> 
$(document).ready(function(){ 
<?php global $frm_entry_meta; 
$entries = $frm_entry_meta->get_entry_metas_for_field(37, $order=''); ?> 
var data = new Array(); <?php foreach($entries as $value){ ?> 
data.push(unescape('<?php echo rawurlencode($value); ?>'); 
<?php } ?> 
$("#field_name-of-the-school").autocomplete(data); }) 
</script> 

// 37 is the field id I want to pull from in the database, 
// and #field_name-of-the-school is the css id 
// for the text field in the form where a user can enter the name of a school. 
// the data for that field is stored in the db under field id 37. 
// other fields, like school id, have their own field ids. 

我的受訪者建議增加data.push(unescape('<?php echo rawurlencode($value); ?>');位。不幸的是,它仍然無法正常工作。

順便說一句,代碼位於page.php的主體中,wordpress用於生成靜態頁面的模板(表單位於其中之一)。

我會認真,真誠地感謝任何和所有幫助。如果這種方法是死路一條(在以前的文章中,還有其他兩個答案都超出了我的頭),我會更願意學習一些新的技巧(儘管它會真正幫助指向相關的學習資源)

在此先感謝。使用上述可能不是你想要啥子如果$entries是一個關聯數組,而不是一個數字的索引的一個

<script> 
$(document).ready(function(){ 
    <?php global $frm_entry_meta; 
    $entries = $frm_entry_meta->get_entry_metas_for_field(37, $order=''); ?> 
    var data = <?php echo json_encode($entries); ?>; 

    $("#field_name-of-the-school").autocomplete({source: data}); 
}); // note you were missing a semicolon at the end of this, which i added 
</script> 

當然:

回答

0

我僅僅指剛使用json_encode和簡化它。如果多數民衆贊成的情況下,你可以做json_encode(array_values($entries));

0

你必須使用

$("#field_name-of-the-school").autocomplete({ source : data }); 

如圖所示Autocomplete documentation例子。你也可能認爲 關於使用JSON