【问题标题】:PDO::FETCH_ASSOC to JqueryPDO::FETCH_ASSOC 到 Jquery
【发布时间】:2012-02-09 13:22:51
【问题描述】:

我需要从关联数组 (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);

【问题讨论】:

    标签: jquery arrays pdo textfield


    【解决方案1】:

    您似乎混淆了服务器端和客户端操作。 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-attributes 匹配时才有效,例如:$sport['full-name'] and &lt;input id="input-full-name" /&gt;。这使得使用它变得更加容易。

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2017-09-11
      • 2023-03-04
      • 2015-04-20
      • 1970-01-01
      相关资源
      最近更新 更多