2010-12-01 56 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

相关问题