PDO::FETCH_ASSOC 到 Jquery

发布于 2025-01-04 04:29:09 字数 281 浏览 2 评论 0原文

我需要从关联数组(PDO::FETCH_ASSOC)中获取数据 并将其放入下面的 3 个空文本字段中。 谁能帮我一些代码。 提前致谢。

尝试了下面的例子,没有运气:

$("#input-full-name").val(sport.values.full_name);
$("#input-short-name").val(sport.values.short_name);
$("#input-abb-name").val(sport.values.abbreviation);

I need to fetch data from an associative array (PDO::FETCH_ASSOC)
and put it in 3 empty textfields below.
Can anyone help me with some code.
Thanks in advance.

Tried the example below, no luck:

$("#input-full-name").val(sport.values.full_name);
$("#input-short-name").val(sport.values.short_name);
$("#input-abb-name").val(sport.values.abbreviation);

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

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

发布评论

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

评论(1

阳光下的泡沫是彩色的 2025-01-11 04:29:09

您似乎混淆了服务器端和客户端操作。
PDO在服务器端运行并与数据库服务器通信。
JavaScript(在本例中为 jQuery)在客户端运行并在 DOM 上运行。
AJAX 是这两者之间的一种联系。

因此,如果您想使用数据库中的某些值填充输入字段,您可以在服务器端执行此操作:

<?php
//run query, fetch array and store it in $sport
?>

<input type="text" name="full_name" value="<?php echo $sport['full-name']; ?>" />
<input type="text" name="short_name" value="<?php echo $sport['short-name']; ?>" />
<input type="text" name="abbreviation" value="<?php echo $sport['abb-name']; ?>" />

或者您可以在客户端执行此操作(例如,如果用户单击链接/按钮/其他内容):

<?php
//this is the script you make an AJAX-request to, e.g. get_data.php

//run query, fetch array and store it in $sport

echo json_encode($sport); //echo the array in JSON-format

这是为用户提供的页面:

...
  <input type="text" name="full_name" id="input-full-name" value="" />
  <input type="text" name="short_name" id="input-short-name" value="" />
  <input type="text" name="abbreviation" id="input-abb-name" value="" />
...
<button id="populate_btn">Populate Data</button>

<scrip>
  $('#populate_btn').click(functiion(){
    $.post('get_data.php', {}, function(sport) {
       //sport now contains the json-array
       $.each(sport, function(key, value) {
         $('#input-'+key).val(value);
       });
    });
  });
</script>

这是一个非常基本的示例,但我希望您能理解。另请注意,给定的 AJAX 示例仅在数组的键与输入字段的 id 属性以某种方式匹配时才有效,例如:$sport['full-name']和 。这使得使用它变得更加容易。

It seems like you're confusing server-side and client-side actions.
PDO runs on the server side and communicates with the database-server.
JavaScript (in this case jQuery) runs on the client side and operates on the DOM.
AJAX is kind of a connection between those two.

So if you want to populate input-fields with some values from the database, you can either do it on the server side:

<?php
//run query, fetch array and store it in $sport
?>

<input type="text" name="full_name" value="<?php echo $sport['full-name']; ?>" />
<input type="text" name="short_name" value="<?php echo $sport['short-name']; ?>" />
<input type="text" name="abbreviation" value="<?php echo $sport['abb-name']; ?>" />

Or you can do it on the client-side (e.g. if the user clicks a link/button/whatever):

<?php
//this is the script you make an AJAX-request to, e.g. get_data.php

//run query, fetch array and store it in $sport

echo json_encode($sport); //echo the array in JSON-format

This is the page served to the user:

...
  <input type="text" name="full_name" id="input-full-name" value="" />
  <input type="text" name="short_name" id="input-short-name" value="" />
  <input type="text" name="abbreviation" id="input-abb-name" value="" />
...
<button id="populate_btn">Populate Data</button>

<scrip>
  $('#populate_btn').click(functiion(){
    $.post('get_data.php', {}, function(sport) {
       //sport now contains the json-array
       $.each(sport, function(key, value) {
         $('#input-'+key).val(value);
       });
    });
  });
</script>

It's a very basic example, but I hope you get the idea. Also note that the given AJAX example only works if the keys of the array match the id-attributes of the input-fields in some kind, e.g.: $sport['full-name'] and <input id="input-full-name" />. This makes it much easier to work with it.

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