【问题标题】:How to pre populate a forms fields from a database using a link?如何使用链接从数据库中预填充表单字段?
【发布时间】:2015-02-09 13:37:22
【问题描述】:

好的,我有一个 html 表单。此表格用于收集用户/会员信息。它使用具有重要“user_email”和“invoice_id”的多个列 2 的数据库。在我的表单中,“user_email”输入是隐藏的,并且在页面加载时等于登录用户在文件中提供的电子邮件地址的值。当用户提交多个表单提交时,我的数据库中会创建多个具有相同 user_email 的记录。例如,如果 user1 提交了 3 个表单,那么我的数据库中将有 3 条记录,它们的“user_email”列值都等于登录用户 1 的电子邮件的值。试图找出一种显示链接列表的方法,单击该链接列表时,将使用数据库中可用的每条记录的信息预先填充表单。因此,如果 user1 已登录并有 3 条记录,则会生成/显示 3 个链接,并且单击每个链接时,都会使用数据库中的记录数据预先填充表单。这就是“invoice_id”输入的来源......生成的链接需要用每个特定记录的用户“invoice_id”值的值/文本进行标记或标题。请注意,“user_id”输入值已经自动生成并在页面加载时预先填充了唯一的序列号...

任何帮助或指出正确的方向来实现这一点都会很棒。谢谢。

<form action="xxx.php" class="well" id="xxx" name"xxx" method="post">


<input type="hidden" id="user_email" name="user_email" value="xxx@email.com">
<input type="text" id="invoice_id" name="invoice_id">
<input type="text" id="other1" name="other1">
<input type="text" id="other2" name="other2">

<input type="submit" value="Submit">

【问题讨论】:

    标签: javascript jquery mysql database forms


    【解决方案1】:

    您需要一些 Ajax 代码,使用 JQuery 将查询发送到您的服务器端(PHP?),然后根据电子邮件地址返回数据库查询的结果。结果可以作为 HTML 加载到 div 中,以便服务器端进行所有格式化等。

      function Lookup() {
        var Options = {
        };
          Options.email= ... email address from form
          $.ajax({
              url: 'Lookup.php',
              type: 'POST',
              data: Options
           }).done(function(data){
                          $("#ResultDiv").html(data);
            });
        }
    

    页面加载后需要调用这个 javascript 函数:

        $(document).ready(function(){
       Lookup();
        }
    

    PHP 代码如下所示:

    ... database connect stuff
     $sSQL     = "SELECT * FROM people WHERE user_email = '".$_POST['email']."';";
    
      $result = RunQuery($sSQL);
    
    if (mysqli_num_rows($result)>0) {
    
      $iLoop    = 0;
      //output results from database
      echo 'OK<table><tr>';
    
      while (($iLoop <= 30) && ($aRow = mysqli_fetch_array($result))) {
        extract($aRow);
    
       echo "<tr><td><a href='".$invoice_id.".html'>".$invoice_id."</a></td><td>"
      ... other info as   required ...."</td></tr>";
    
        $iLoop++;
    
      }
      echo '</tr></table>';
    

    【讨论】:

      【解决方案2】:

      试试这个:

       // Get user from server
                  $.post("GetUserData", data, function (result, status) {
                      // Loop through each field in the returned object
                      $.each(result, function (key, element) {
                          // Append the field value to the 
                          // correct field inside the form
                          $("#user_form :input[name = '" + key + "']").val(element);
                      });
                  })
                  .fail(function (error) {
                      // Retrieve the error response from the title
                      retrieveErrorTitle(error);
                  });
      

      【讨论】:

        猜你喜欢
        • 2011-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-25
        相关资源
        最近更新 更多