javascript - 提交后在 php 中添加文本行

标签 javascript php html click

<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

再次刷新页面后。

如何在点击后添加文本?​​

最佳答案

虽然我不确定您到底想要什么,但以下(非常基本的)代码示例可能会对您有所帮助。

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

如果您想参加 session ,请仔细阅读 manual ,还有很多重要的细节没有包含在这个答案中。

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

注意:我添加了一个按钮以从头开始(重新)开始(清除用户输入)。

关于javascript - 提交后在 php 中添加文本行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59698735/

相关文章:

php - 从浏览器启动在后台执行 php 脚本

javascript - 动态搜索div列表

html - 如何在 span 标签内实现 block 链接?

javascript - Karma 使用 externalUrl 单元测试 Angular JS 指令

javascript - ionic 2 : disable scrolling on login form input focus

javascript - 判断一个 div 是否有滚动条

php - 点击外部链接后链接到页面特定位置

php - 在laravel中自动检测登录用户的右后卫

jquery - Bootstrap 3 带按钮的旋转木马

javascript - 使用 python 为基于 Web 的 ubuntu 终端编写伪终端