0
<html>
<head><title>some title</title></head>
<body>
  <form method="post" action="">
    <input type="text" name="test1" value="<?= isset($_POST['test']) ? htmlspecialchars($_POST['test']) : '' ?>" />
    <input type="submit" name="submit" />
  </form>

<?php
if(isset($_POST['submit'])) {
  echo 'You entered: ', htmlspecialchars($_POST['test']);
}
?>
</body>
<html>

After the click i want add new line automatically.

If now I have:

You entered: test1

After another click my text is 'test2 I have:

You entered: test1
You entered: test2

After refresh page all over again.

How do I add text after a click?

5
  • 1
    If you want the "You entered..." values to persist between clicks, so that you can have more than one of them displayed at once, then you need to store them somewhere (because HTTP is stateless by nature, and values are not preserved between requests by default). For this purpose, it sounds like the Session would be a sensible place to store it. (if you wanted more long-term storage, or for data to be shared between different users of your site, then file or database storage would be more appropriate forms of data persistence). Commented Jan 11, 2020 at 21:24
  • My suggestion to use JavaScript for this case. Commented Jan 11, 2020 at 21:44
  • @SlavaRozhnev that assumes the data doesn't also need to be transmitted to the server for some purpose. The example code above is almost certainly simplified from what's really needed, because otherwise the program would be fairly pointless. Commented Jan 11, 2020 at 22:06
  • JavaScript can be used for submit data to server as well. You can use Ajax technique for post data to server and instantly, without page refresh. Commented Jan 11, 2020 at 22:18
  • @SlavaRozhnev yes you can, but that's just extra work, and in this case there's no obvious purpose to it, when there's a much quicker solution using the Session. Commented Jan 12, 2020 at 13:46

1 Answer 1

1

Although I'm not sure what it is you're after exactly, the following (very basic) code sample might serve to help you on your way.

It uses PHP sessions, but as highlighted in the comments, there are other ways of data persistence you might want to investigate.

If you want to go with sessions, do read through the manual, there are a lot of important details that are not covered by this answer.

<?php
session_start();
?>

<html>
<head><title>some title</title></head>
<body>
<form method="post" action="">
    <input type="text" name="test1" value="<?= isset($_POST['test1']) ? htmlspecialchars($_POST['test1']) : '' ?>"/>
    <input type="submit" name="submit"/>
    <input type="submit" name="clear-input" value="clear">
</form>
</body>
<html>

<?php
if (isset($_POST['submit'])) {
    $_SESSION['input'][] = $_POST['test1'];
    foreach ($_SESSION['input'] as $input) {
        echo 'You entered: ' . htmlspecialchars($input);
        echo '<br />';
    }
}
if(isset($_POST['clear-input'])) {
    $_SESSION['input'] = [];
}
?>

note: I added a button to (re)start with a clean slate (clear user input).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.