javascript - 提交后在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
重新刷新页面后。
点击后如何添加文字?
解决方案
尽管我不确定您到底在追求什么,但以下(非常基本的)代码示例可能会在您的道路上为您提供帮助。
它使用 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'] = [];
}
?>
注意:我添加了一个按钮来(重新)从一个干净的状态开始(清除用户输入)。
推荐阅读
- jmeter-plugins - 为什么 HLS 采样器错误显示空白错误
- android - 不幸的是,应用程序已停止 Facebook 登录
- r - 使用 if 语句的 gsub 函数
- excel - Excel CSV 导入:QueryTable.FillAdjacentFormulas 仅适用于第二次刷新
- php - 从没有allow_url_include的远程php函数获取变量
- azure-ad-b2c - Azure B2C 本地帐户使用用户名注册 - 如何获取用户名建议
- linux - pthread_create 在 Linux 中分配大量内存?
- python - IP Whitelist Function - Flask - Python3.x
- c# - jquery ajax发布后模型不更新
- react-native - Vimeo API 在一个 GET 请求中获取多个视频