如何将表单日期添加到html表

时间:2014-03-02 10:14:19

标签: php

您好我想将表单数据发送到php确认页面。确认页面应包含注册人的详细信息。新注册人的详细信息应添加到之前注册的注册人。这是我所做的,但不是向表中添加新信息,而是替换它。我是php的新手,我需要有关如何绕过它的建议。提交后它应该是这样的:

--------------------------
Registration Conformation |
---------------------------------------------
First name    |Last name     |Email         |
---------------------------------------------
Jack          |Fams          |mail@time.com |
---------------------------------------------
Matthew       |Bills         |mail@time.com |
---------------------------------------------
Judith        |Kamps         |mail@time.com |
---------------------------------------------




     <html>
        <head>
        <title>Registration form
        </title>
        <style type="text/css">

        .button{

            box-shadow: 10px 10px 5px #888888;
            padding: 5px;
            width: 90%;
        }

        .div{

            border: 2px solid black;
            width: 28%;
            margin: auto;
            margin-top: 15%;
            padding: 10px;
            font-family: Comic Sans;
            text-align: center;
        }

        h1{
            text-align: center;
            font-family: "Comic Sans", "Comic Sans MS", cursive;
            font-weight: bold;
        }

        table.set{

            text-align: center;
            margin-left: auto;
            margin-right: auto;
            width: 60%;
        }

        td{
            text-align: left;
            font-family: "Comic Sans", "Comic Sans MS", cursive;
            font-weight: bold;
        }

        </style>
        </head>
        <body>
            <div class="div">
            <form method="POST" action="registry.php">
            <h1>NewsLetter-Registration</h1>
            <table class="set">
            <tr><td>First Name:</td><td><input type='text' name='fname' size="25" /></td></tr>
            <tr><td>Last Name:</td><td><input type='text' name='lname' size="25"/></td></tr>
            <tr><td>E-mail:</td><td><input type='text' name='email' size="25"/></td></tr>
            <tr><td></td><td><input class="button" type="submit" name="register" value="Register"></td></tr>
            </table>
            </form>
            </div>
        </body>
    </html>


registry.php

<html>
    <head>
    <title>Registration form
    </title>
    <style type="text/css">

    .button{

        box-shadow: 10px 10px 5px #888888;
        padding: 5px;
        width: 90%;
    }

    .div{
        width: 60%;
        margin: auto;
        margin-top: 15%;
        padding: 10px;
        font-family: Comic Sans;
        text-align: center;
    }

    h1{
        text-align: left;
        font-family: "Comic Sans", "Comic Sans MS", cursive;
        font-weight: bold;
    }

    table.set{

        text-align: center;
        margin-left: auto;
        margin-right: auto;
        width: 60%;
        border-collapse: collapse;
        border: 2px solid black;
    }

    td{
        text-align: left;
        font-family: "Comic Sans", "Comic Sans MS", cursive;
        font-weight: bold;
    }

    </style>
    </head>
    <body>
        <div class="div">
        <table class="set" border="1">
        <tr><td colspan="3"><h1>NewsLetter-Registry</h1></td></tr>
        <tr><td>First Name:</td><td>Last Name:</td><td>E-mail</td></tr>
            <tr><td><?php echo $_POST["fname"]; ?></td><td><?php echo $_POST["lname"]; ?></td><td><?php echo $_POST["email"]; ?></td></tr>;
        </table>
        </div>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

<强>问题

目前,您只是将表单数据发送到新页面并将其打印在表格中。它不存储在任何地方。

如果你想在没有数据库的情况下实现这一点,你可以使用我们可以“数据文件”的csv或json文件

  1. 将表单提交至registry.php
  2. 将新条目附加到数据文件
  3. 加载数据文件并使用“while”或“loop”将数据打印到表格中
  4. 一个简单方法的快速示例(不是最佳方式) - 对于大数据,追加或数据库将是您的最佳解决方案。

    <强> registry.php

    <?php
        $datafile = "data.json";
        $data = json_decode(file_get_contents($datafile),1); //make sure to create data.json empty file first
    
        if(isset($_POST['fname'])){
            $data[] = array(
                'fname' => $_POST['fname'],
                'lname' => $_POST['lname'],
                'email' => $_POST['email']
            );
    
           file_put_contents($datafile, json_encode($data,1)); //save new file
        }
    ?>
    

    然后您可以打印表格:

        <table class="set" border="1">
        <tr><td colspan="3"><h1>NewsLetter-Registry</h1></td></tr>
        <tr><td>First Name:</td><td>Last Name:</td><td>E-mail</td></tr>
        <?php
        foreach($data as $i=>$entry){
        <tr><td><?php echo $entry["fname"]; ?></td>
        <td><?php echo $entry["lname"]; ?></td>
        <td><?php echo $entry["email"]; ?></td></tr>
        } ?>
        </table>
    

    这是一个基本的例子,未经测试,所以如果有错误让我知道,我会更新代码。你需要