[提交后在php中添加文本行

问题描述 投票:0回答:1
<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

重新刷新页面后。

单击后如何添加文本?​​

javascript php html click
1个回答
0
投票

尽管我不确定您到底要做什么,但是以下(非常基本的)代码示例可能会为您提供帮助。

它使用PHP sessions,但是正如注释中突出显示的那样,您可能还需要研究其他数据持久性的方式。

如果您想参加会议,请仔细阅读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'] = [];
}
?>

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

© www.soinside.com 2019 - 2024. All rights reserved.