为什么我复选框的数据提交不上去

if ($this->request->isPost()) {
// 表单数据
$data = $this->request->post();
if (Db::name('user')->insert($data)) {

$this->success('新增成功', 'index');
} else {
$this->error('新增失败');
}
}

$city = ['1' => '广州', '2' => '北京', '3' => '上海'];

return ZBuilder::make('form')->setPageTitle('添加')
->addText('name', '姓名')
->addCheckbox('city', '城市', '选择城市', $city,'')
->fetch();
提交后名字能写入数据库,但城市没有写入。 类型设置的是 text
已邀请:

dolphinphp

赞同来自: laozhu1986

复选框提交到服务器是以数组形式的,你需要自行转换一下,比如
 
if ($this->request->isPost()) {
// 表单数据
$data = $this->request->post();
if (isset($data['city'])) {
$data['city'] = implode(',', $data['city']);
} else {
$data['city'] = '';
}
if (Db::name('user')->insert($data)) {

$this->success('新增成功', 'index');
} else {
$this->error('新增失败');
}
}

$city = ['1' => '广州', '2' => '北京', '3' => '上海'];

return ZBuilder::make('form')->setPageTitle('添加')
->addText('name', '姓名')
->addCheckbox('city', '城市', '选择城市', $city,'')
->fetch();

要回复问题请先登录注册