亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開發(fā)留言板教程之添加留言

下面我們來看以下html代碼:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>留言板</title>
    <style type="text/css">
        *{margin:0px;padding:0px;}
        body{background:#eee;}
        #bdy{width:414px;height:736px;margin:0 auto;margin-top:20px;
            background:#66CDAA;
        }
        #top{font-family:"隸書";font-size:30px;text-align:center;/*margin-top:18px;*/
            color:#f60;}
        .a{text-decoration:none;color:#fff;float:right;padding-right:15px;}
        .a:hover{color:red;}
        #cont{width:414px;height:736px;margin:0 auto;margin-top:20px;}
        #left{width:350px;height:300px;margin-top:80px;margin-left:15px;/*float:left;*/
            background:#48D1CC;padding-left:5px;}

        #right{width:360px;height:200px;margin-top:20px;background:#48D1CC;
            margin-left:15px;/*float:left;*/}
        h5{text-align:center;margin-top:15px;margin-bottom:20px;}
        #sub{width:120px;height:25px;margin-top:15px;}
        #sub:hover{background:#AFEEEE;}
        .span{font-size:18px;color:red;font-weight:bold;}
        table{width:360px;margin:0 auto;margin-top:15px;border:1px solid #eee;}
        td{text-align:center;}
        #td a{text-decoration:none;color:#eee;}
        #td a:hover{color:red;}
    </style>
</head>
<body>
    <div id="bdy">
        <div id="top">留言板</div>
        <a href="login.php" class="a">登錄</a>
        <a href="reg.php" class="a">注冊(cè)</a>
        <div id="cont">
            <div id="left">
                <h5>寫留言</h5>
                <form method="post" action="addmessage.php">
                    標(biāo)題:<input type="text" placeholder="請(qǐng)輸入標(biāo)題" name="title">
                    </br></br>
                    內(nèi)容:<textarea cols="40" rows="5" name="content"></textarea>
                    </br></br>
                    <input type="submit" value="添加留言" id="sub">
                </form>
            </div>
            <div id="right"></div>
        </div>
    </div>
</body>
</html>

表單提交到addmessage.php,下面我們具體來看以下addmessage.php文件

首先也要打開session

session_start();

注:此時(shí)打開seession 為了后面我們進(jìn)行判斷,如果登錄了,即可留言,反之,不可留言

引入連接數(shù)據(jù)庫文件 conn.php

require_once('conn.php');

設(shè)置字符編碼

header("Content-type: text/html; charset=utf-8");//設(shè)置編碼

下面我們要獲取表單的信息

????$title = $_POST['title'];
?? ?$content = $_POST['content'];
?? ?$messtime = time();

接下來我們要進(jìn)行添加留言的操作了

如果沒有填寫標(biāo)題和內(nèi)容,我們應(yīng)該不讓他提交

????if(empty($title)){
?? ??? ?echo "<script>alert('請(qǐng)輸入標(biāo)題');history.go(-1);</script>";
?? ?}elseif(empty($content)){
?? ??? ?echo "<script>alert('請(qǐng)輸入內(nèi)容');history.go(-1);</script>";
?? ?}

否則的話,就應(yīng)該可以添加,如下代碼:

????if(!empty($_SESSION['name'])){
?? ??? ??? ?$sql = "insert into mess (title,content,messtime) values('$title','$content','$messtime')";
?? ??? ??? ?$result =mysql_query($sql);
?? ??? ??? ?if($result){
?? ??? ??? ??? ?echo "<script>alert('添加留言成功');location.href='message.php';</script>";
?? ??? ??? ?}else{
?? ??? ??? ??? ?echo "<script>alert('添加留言失敗');history.go(-1);</script>";
?? ??? ??? ?}
?? ??? ?}else{
?? ??? ??? ?echo "<script>alert('請(qǐng)登錄后添加留言');history.go(-1);</script>";
?? ??? ?}

對(duì)session['name']進(jìn)行判斷,如果不為空,說明已經(jīng)登錄,那么我們應(yīng)該可以添加留言,否則的話,就提示讓用戶登錄后留言


完整代碼如下:

<?php
    session_start();
    header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
    require_once('conn.php');
    $title = $_POST['title'];
    $content = $_POST['content'];
    $messtime = time();

    if(empty($title)){
        echo "<script>alert('請(qǐng)輸入標(biāo)題');history.go(-1);</script>";
    }elseif(empty($content)){
        echo "<script>alert('請(qǐng)輸入內(nèi)容');history.go(-1);</script>";
    }else{
        if(!empty($_SESSION['name'])){
            $sql = "insert into mess (title,content,messtime) values('$title','$content','$messtime')";
            $result =mysql_query($sql);
            if($result){
                echo "<script>alert('添加留言成功');location.href='message.php';</script>";
            }else{
                echo "<script>alert('添加留言失敗');history.go(-1);</script>";
            }
        }else{
            echo "<script>alert('請(qǐng)登錄后添加留言');history.go(-1);</script>";
        }
    }
?>


繼續(xù)學(xué)習(xí)
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>留言板</title> <style type="text/css"> *{margin:0px;padding:0px;} body{background:#eee;} #bdy{width:414px;height:736px;margin:0 auto;margin-top:20px; background:#66CDAA; } #top{font-family:"隸書";font-size:30px;text-align:center;/*margin-top:18px;*/ color:#f60;} .a{text-decoration:none;color:#fff;float:right;padding-right:15px;} .a:hover{color:red;} #cont{width:414px;height:736px;margin:0 auto;margin-top:20px;} #left{width:350px;height:300px;margin-top:80px;margin-left:15px;/*float:left;*/ background:#48D1CC;padding-left:5px;} #right{width:360px;height:200px;margin-top:20px;background:#48D1CC; margin-left:15px;/*float:left;*/} h5{text-align:center;margin-top:15px;margin-bottom:20px;} #sub{width:120px;height:25px;margin-top:15px;} #sub:hover{background:#AFEEEE;} .span{font-size:18px;color:red;font-weight:bold;} table{width:360px;margin:0 auto;margin-top:15px;border:1px solid #eee;} td{text-align:center;} #td a{text-decoration:none;color:#eee;} #td a:hover{color:red;} </style> </head> <body> <div id="bdy"> <div id="top">留言板</div> <a href="login.php" class="a">登錄</a> <a href="reg.php" class="a">注冊(cè)</a> <div id="cont"> <div id="left"> <h5>寫留言</h5> <form method="post" action="addmessage.php"> 標(biāo)題:<input type="text" placeholder="請(qǐng)輸入標(biāo)題" name="title"> </br></br> 內(nèi)容:<textarea cols="40" rows="5" name="content"></textarea> </br></br> <input type="submit" value="添加留言" id="sub"> </form> </div> <div id="right"></div> </div> </div> </body> </html>
提交重置代碼