我現(xiàn)在有個添加頁面,用的checkbox,想把選中的值傳進數(shù)據(jù)庫,應該怎么做,希望有詳細答案,本人小白,太高深理解不了。最好直接上代碼這是代碼?<tr>? ? ? <td height="26" align="right">興趣:</td>? ? ? <td height="26"><input type="checkbox" name="hobby" value="乒乓球" />乒乓球</td>? ? ? <td height="26"><input type="checkbox" name="hobby" value="羽毛球" />羽毛器</td>? ? ? <td height="26"><input type="checkbox" name="hobby" value="保齡球" />保齡球</td>? ? </tr>
1 回答
已采納

DemiMurphy
TA貢獻3條經(jīng)驗 獲得超1個贊
視圖代碼:
<form?method="post"?action="{:U('add')}"> ????<tr> ??????????<td?height="26"?align="right">興趣:</td> ??????????<td?height="26"> ??????????????<input?type="checkbox"?name="hobby[]"?value="乒乓球"?/>乒乓球 ??????????</td> ??????????<td?height="26"> ??????????????<input?type="checkbox"?name="hobby[]"?value="羽毛球"?/>羽毛器 ??????????</td> ??????????<td?height="26"> ??????????????<input?type="checkbox"?name="hobby[]"?value="保齡球"?/>保齡球 ??????????</td> ????</tr> </form>
控制器代碼:
public?function?add(){ ????$hobby?=?I('post.hobby'); ????$hobby?=?json_encode($hobby); ????try{ ????????if(M('table_name')->add($hobby)){ ????????????$this->success('新增成功'); ????????}else{ ????????????throw?new?\Exception("新增失敗"); ????????} ????} ????catch(\Exception?$e){ ????????$this->error($e->getMessage.$this->getLastSql()); ????} }
大概思路如上,代碼也是 ThinkPHP 3.2 的代碼
- 1 回答
- 0 關注
- 1379 瀏覽
添加回答
舉報
0/150
提交
取消