douhui5529 2014-03-24 16:41 采纳率: 100%
浏览 81
已采纳

如何使用数据库中的数据预填充Web表单

I have a form (actually its 4 different forms on 4 consecutive pages, but to the same effect) which users fill in to sign up to a website. There are many (100+) items in the form, including text boxes, ranges, but mostly checkboxes.

Once the user has signed up I want them to be able to edit their preferences via a simple 'edit' link on their custom homepage. My idea is that they will simply be shown the sign-up forms again, but this time they will be pre-populated using the data linked to their user account from my database.

Is this as simple as the 'edit' link in question containing a POST value to pass to the form pages:

http://www.mysite.com/something.php?edit=true

And then checking for this value in the php on the form page, and setting the values in each form item if edit is true?

The reason im double-checking on this is because it seems an awful lot of code to go through each item in the form and set its value to whatever is in the database and I dont want to do it just to find out later down the line there is a quicker and less programming intensive way of doing this!

In case it helps to find an alternative solution, Im using PDO, the user is identified by way of a session variable containing their (unique) email address, and the form names are identical to the database column names.

  • 写回答

2条回答 默认 最新

  • dqbh8054 2014-03-24 16:56
    关注

    I would put the form inside a separate php file so it can be reused by different php scripts.

    Inside this separate php file I would set the input values to POST variables.
    I.e. (for this example we'll call this file myform.php)

    <form action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES); ?>" method="POST">
        <input type="text" name="first_name" value="<?php echo @$_POST['first_name']; ?>" >
        <input type="text" name="last_name" value="<?php echo @$_POST['last_name']; ?>" >
        <input type="text" name="username" value="<?php echo @$_POST['username']; ?>" >
        <input type="submit" name="submit" value="<?php echo $submit_value; ?>" >
    </form>
    


    And then on the PHP side for the edit page have (this assumes your DB fields match your input field names)

    <?php
    foreach($db_row_fields as $field_key => $field_value)
    {
         $_POST[$field_key] = $field_value;
    }
    
    $submit_value = "Update";
    
    include 'myform.php';
    

    Obviously only get the fields you need out of the DB.


    For your sign up you only need to set $submit_value as you want the POST variables to be empty

    $submit_value = "Sign Up";
    
    include 'myform.php';
    

    While you are using two different PHP scripts to handle sign up and editing also make validation and form processing code as reusable as possible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?