如何允许用户在页面上提交同一表单的多个实例并使用 php 存储数据
How do I allow a user to submit multiple instances of the same form on a page and store the data using php?
我正在开发一个由html表单组成的php项目,该表单允许用户提交文本,歌曲名称,作曲家和艺术家。用户填写表单并单击"提交"后,应存储数据并允许再次填写表单,直到用户按下另一个按钮,该按钮显示已提交的所有数据。到目前为止,我已经考虑使用数组,但我不确定这将如何与发送到同一数组的多个表单提交一起工作。任何帮助将不胜感激。
<html>
<head>
</head>
<body>
<form method="post">
Name of song: <input type="text" name="songName"><br>
Composer: <input type="text" name="composer"><br>
Artist/Group: <input type="text" name="artist"><br>
<input type="submit" name="submit">
</form>
</body>
<?php
if (!empty($_POST['submit'])) {
//Submit the data into the array or something here
}
?>
</html>
当然,试试这个,看看会发生什么:
<?php
session_start();
// Initialize an array for answers
if (!isset($_SESSION['answers']))
$_SESSION['answers'] = array();
?>
<html>
<head>
</head>
<body>
<form method="post">
Name of song: <input type="text" name="songName"><br>
Composer: <input type="text" name="composer"><br>
Artist/Group: <input type="text" name="artist"><br>
<input type="submit" name="submit">
</form>
</body>
<?php
if (!empty($_POST['submit'])) {
// Push the posted data into the session array
$_SESSION['answers'][] = $_POST;
}
// Display the data now
foreach($_SESSION['answers'] as $array) {
echo "Name of song: {$array['songName']}<br>";
echo "Composer: {$array['compose']}<br>";
echo "Artist/Group: {$array['artist']}<br><hr>";
}
?>
</html>
注意:会话仅保留到用户注销或超时。为了长时间持久化,您需要使用 MySQL 等数据库来存储答案
相关文章:
- 将新类实例中的数据存储在数组中
- 删除数据网格 (Dojo) 和表 (数据网格) 的实例正确显示
- 猫鼬数据不会保存模型的新实例
- 当有多个 React.js 组件实例具有数据请求时如何处理缓存
- 主干 - 实例化视图包含旧数据
- 将数据表 API 实例存储在对象中
- 如何允许用户在页面上提交同一表单的多个实例并使用 php 存储数据
- 获取在视图实例中定义的具有特定 ID 的数据
- 重新访问了 Python 私有实例数据
- 访问模板的实例范围并注入数据
- 在同一控制器(ng-controller)的不同实例之间共享数据
- 实例检查附加到数据属性的 jQuery 插件实例
- 《烈焰流星》动态实例化模板和数据上下文
- d3.js单击时更改数据(圆环图的多个实例)
- 如何删除多维数据集.js实例的一天之后的事件
- 如何在集成测试中访问ember数据存储实例
- 如何在使用数据表时替换Knockout中的数组实例
- 如何停止数据表实例已启动的所有当前正在进行的 Ajax 查询
- 如何从autoform钩子访问模板实例数据
- 将模板实例数据作为关键字参数传递给嵌套模板