標籤:
書接前文。
我想前台填寫內容,然後在後台儲存一下,規划了一下前台要錄入的內容,主要包括title、content兩大內容。然後簡單設計了一個前台頁面(複雜的我得會啊),就在上篇文章的基礎上直接加了:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>ThinkPHP</title> </head> <body> <div class="main"> <h2>{$hello}</h2><form method=‘post‘ action="__URL__/add"> <table cellpadding=2 cellspacing=2> <tr> <td >標題:</td> <td ><input type="text" name="title" ></td> </tr> <tr> <td >內容:</td> <td><textarea name="content" rows="5" cols="25"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" class="button" value="提 交"> <input type="reset" class="button" value="清 空"></td> </tr> </table> </form></div> </body></html>
然後呢,在後台資料庫中建立一個表,注意資料配置中的表首碼,包括兩個欄位就可以:title,content,當然,最好加上個自增長的id.
背景IndexAction.class.php簡單增加一個方法:
<?phpclass IndexAction extends Action{ public function index(){ $hello=‘Hello,ThinkPHP !‘; $this->assign(‘hello‘,$hello); $this->display(); } public function add(){ $form = M(‘Form‘); $form->create(); $form->add(); }}
對,就是文中的那個簡單的add()方法,好了,重新整理前台頁面,隨便錄入一些可笑的文字,然後提交....
快到後台開啟資料庫看一下吧,剛才填寫的資料......竟然儲存進去了!
後台代碼我們沒有明確的寫資料儲存,甚至沒有欄位的映射!這是真的嗎?
是真的,就這麼簡單,當然,實際中我們可能需要很多的檢驗處理,比如合法性錄入、重複檢查等等。但如果簡單的,就這樣!
(不過在實際情況中大部分都需要自己做一些處理的,什麼都交出去有時總不讓人放心不是?)
好了,這次的內容就到這裡了。
ThinkPHP學習(二)