使用PHP发布多行

时间:2016-11-17 15:44:10

标签: php

当有人按下提交按钮时,会添加一行代码:

<html>
<head>
</head>
<body>

    <form method ="POST" action="" name ="formpje">

        A line<input type="text" name="name"><br>
        <input type="submit" name="submit"><br>
    </form>

<?php
$post = $_POST['name'];
echo $post;
?>
</body>
</html>

结果:

enter image description here 当我添加新行时,当前的行将更改为最新的行。我希望它留下来,因为我添加一个新的。 下面是它的样子:

enter image description here

1 个答案:

答案 0 :(得分:4)

试试这个,每次提交表单时,它都会将名称放入会话中并将其打印出来。

<?php 
    session_start();
    if( isset( $_POST[ 'submit' ] ) )
    {  
        $_SESSION[ 'submissions' ][] = $_POST[ 'name' ]; 
    } 
?>
<form method ="POST" action="" name ="formpje">
    A line<input type="text" name="name"><br>
    <input type="submit" name="submit"><br>
</form>

<?php
    foreach( $_SESSION[ 'submissions'] as $line )
    {
        echo $line."\n";  <------ May need to replace this with "<br>" instead of "\n" for the html.
    }
?>