du8791069 2010-12-01 17:26
浏览 28

如何使用PHP将数据提供给jQuery自动完成?

This is a continuation of an earlier post. Unfortunately, The solutions posted didn't work and my follow up questions weren't addressed. Just in case this is because my generous respondents didn't notice I had replied, I'm reposting my problem.


I'm trying to build a form where certain text fields and text areas have autocomplete.

I've used the formidable plugin for wordpress to build my form. I'm using the jQuery autocomplete plugin for the autocomplete part.

After implementing the suggestions of one of my respondents, the code now looks like this:

<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.

My respondent suggested adding the data.push(unescape('<?php echo rawurlencode($value); ?>'); bit. Unfortunately, it's still not working.

BTW, the code is in the body of page.php, a template which wordpress uses to generate static pages (the form is on one of these).

I would seriously, seriously appreciate any and all help with this. If this approach is a dead end (on the earlier posts, there were two other answers that were over my head,) I would be more than willing to learn some new tricks (though it would really help to be pointed to a relevant learning resource.)

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • drgbpq5930 2010-12-01 17:34
    关注

    I would jsut use json_encode and simplify it:

    <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>
    

    Of course using the above may not be waht you want if $entries is an associative array instead of a numeric indexed one. If thats the case you can do json_encode(array_values($entries));

    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗