ThinkPHP 是一个流行的 PHP 开发框架,它提供了丰富的功能和简洁的 API,使得开发者能够快速构建 Web 应用程序。批量提交表单是指在一个表单中提交多个记录或数据项,而不是逐个提交。
以下是一个简单的 ThinkPHP 批量提交表单的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>批量提交表单</title>
</head>
<body>
<form action="/index/batchSubmit" method="post">
<input type="hidden" name="csrf_token" value="your_csrf_token">
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="text" name="data[0][id]" value="1"></td>
<td><input type="text" name="data[0][name]" value="Alice"></td>
<td><input type="text" name="data[0][age]" value="25"></td>
</tr>
<tr>
<td><input type="text" name="data[1][id]" value="2"></td>
<td><input type="text" name="data[1][name]" value="Bob"></td>
<td><input type="text" name="data[1][age]" value="30"></td>
</tr>
</tbody>
</table>
<button type="submit">批量提交</button>
</form>
</body>
</html>
<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
class BatchSubmit extends Controller
{
public function index(Request $request)
{
if ($request->isPost()) {
$data = $request->post('data');
foreach ($data as $item) {
// 处理每一条数据
$id = $item['id'];
$name = $item['name'];
$age = $item['age'];
// 插入数据库或其他处理逻辑
// 例如:$this->model->insert(['id' => $id, 'name' => $name, 'age' => $age]);
}
return json(['status' => 'success', 'message' => '批量提交成功']);
}
return $this->fetch();
}
}
通过以上步骤,你可以实现一个基本的 ThinkPHP 批量提交表单功能,并解决常见的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云