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

发布于 2024-10-05 20:30:40 字数 1430 浏览 1 评论 0原文

这是之前帖子的延续。不幸的是,发布的解决方案不起作用,我的后续问题也没有得到解决。以防万一我慷慨的受访者没有注意到我已回复,我将重新发布我的问题。


我正在尝试构建一个表单,其中某些文本字段和文本区域具有自动完成功能。

我使用了强大的 WordPress 插件来构建我的表单。我使用 jQuery 自动完成插件 作为自动完成部分。

在实施了一位受访者的建议后,代码现在如下所示:

<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('');<不幸的是,它仍然无法正常工作,

其中之一)。

代码位于 page.php 的主体中,这是 WordPress 用来生成静态页面的模板(我非常非常感谢 如果这种方法是一个死胡同(在之前的帖子中,还有另外两个答案超出了我的能力),我将非常愿意学习一些新技巧(尽管这确实有帮助)指向相关的学习资源。)

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与之呼应 2024-10-12 20:30:40

我会使用 json_encode 并简化它:

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

当然,如果 $entries 是一个关联数组而不是数字索引数组,那么使用上面的方法可能不是您想要的。如果是这种情况,您可以执行 json_encode(array_values($entries));

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

以歌曲疗慰 2024-10-12 20:30:40

您必须

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

按照 自动完成文档 示例中所示的方式使用。另外你可能会想
关于使用 JSON

You have to use

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

as shown in the Autocomplete documentation example. Also you might think
about using JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文