[thinkPHP5專案實戰_30]留言板功能完善

騎著程式碼去流浪發表於2017-11-12

實現的功能:開啟留言本頁面展示留言,並提供留言功能:

開啟application/index/controller/Guest.php控制器:

<?php
namespace app\index\controller;
class Guest extends Basic
{
    public function index()
    {
    	
    	if(request()->isPost()){
    		$data=[
    			'nickname'=>input('inpName'),
    			'homepage'=>input('inpHomePage'),
    			'email'=>input('inpEmail'),
    			'content'=>input('txaArticle'),
    			'time'=>time(),
    		];
    		if(\think\Db::name('guest')->insert($data)){
    			return $this->success('留言成功,正在跳轉','Guest/index');
    		}else{
    			return $this->success('留言提交失敗','Guest/index');
    		}
    		return;
    	}

    	$guestres=\think\Db::name('guest')->select();
    	$this->assign('guestres',$guestres);
        return $this->fetch('guest');
    }
}

對應的模板guest.html:

<body>
    {include file="Public/header" /}
    <div class="container">
        <div class="row">
            <div class="col-sm-8 blog-main">
                <div id="divMain">
                    <br>
                    <ul style="list-style: none;">
                        {volist name="guestres" id="vo"}
                        <li>
                            <div>
                                <img class="avatar" src="http://cn.gravatar.com/avatar/75e215cea7a9364665ca688b5a23a58a?s=40&d=mm" alt="" width="32" /> 
                                <strong>{$vo.nickname}</strong>
                            </div>
                            <div style="color: gray;">
                                <small> 釋出於 {$vo.time|date="Y年m月d日 H:i:s",###}  
                            </div>
                            <div style="color: orange;font-style: italic;font-weight: bold;">{$vo.content}</div>
                        </li>
                        <br>
                        {/volist}
                    </ul>
                    <div class="post" id="divCommentPost">
                        <p class="posttop"><a name="comment">發表留言:</a></p>
                        <form id="frmSumbit" target="_self" method="post" action="{:url('guest/index')}">
                            <input type="hidden" name="inpId" id="inpId" value="2" />
                            <input type="hidden" name="inpArticle" id="inpArticle" value="" />
                            <input type="hidden" name="inpRevID" id="inpRevID" value="" />
                            <p>
                                <input type="text" name="inpName" id="inpName" class="text" value="" size="28" tabindex="1" />
                                <label for="inpName">名稱(必填)</label>
                            </p>
                            <p>
                                <input type="text" name="inpEmail" id="inpEmail" class="text" value="" size="28" tabindex="2" />
                                <label for="inpEmail">郵箱</label>
                            </p>
                            <p>
                                <input type="text" name="inpHomePage" id="inpHomePage" class="text" value="" size="28" tabindex="3" />
                                <label for="inpHomePage">網站連結</label>
                            </p>
                            <p>
                                <label for="txaArticle">正文(必填)(留言最長字數:1000)</label>
                            </p>
                            <p>
                                <textarea name="txaArticle" id="txaArticle" cols="50" rows="4" tabindex="5"></textarea>
                            </p>
                            <p>
                                <input name="btnSumbit" type="submit" tabindex="6" value="提交" class="button" />
                            </p>
                        </form>
                        <p class="postbottom">◎歡迎參與討論,請在這裡發表您的看法、交流您的觀點。</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {include file="Public/footer" /}
</body>

對應的資料表tp5_guest結構:


效果:


相關文章