首页 > 解决方案 > 提交后在php中添加文本行

问题描述

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

点击后我想自动添加新行。

如果现在我有:

You entered: test1

再次单击后,我的文本是 'test2 我有:

You entered: test1
You entered: test2

重新刷新页面后。

点击后如何添加文字?

标签: javascriptphphtmlclick

解决方案


尽管我不确定您到底在追求什么,但以下(非常基本的)代码示例可能会在您的道路上为您提供帮助。

它使用 PHP会话,但正如评论中强调的那样,您可能还想研究其他数据持久性方法。

如果您想参加会议,请通读手册,此答案未涵盖许多重要细节。

<?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'] = [];
}
?>

注意:我添加了一个按钮来(重新)从一个干净的状态开始(清除用户输入)。


推荐阅读