Kevin Kevin - 20 days ago 6
PHP Question

Post multiple lines with PHP

When someone presses the submit button a line is added with this code:

<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>


The result:

enter image description here
When i add a new line the current one gets changed to the newest one. I want it to stay as i add a new one.
Heres what it would look like:

enter image description here

Answer

Try this, each time you submit the form it will place the name into a session and print it out bellow.

<?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.
    }
?>