PHP メッセージボード開発チュートリアル - メッセージの追加
次の 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">注册</a>
<div id="cont">
<div id="left">
<h5>写留言</h5>
<form method="post" action="addmessage.php">
标题:<input type="text" placeholder="请输入标题" name="title">
</br></br>
内容:<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_start();
注: この時点でセッションを開きます。後で判断するため、ログインしている場合はメッセージを残すことができ、ログインしていない場合はメッセージを残すことができません
接続データベース ファイル conn.php を導入します
require_once(' conn.php');
文字エンコーディングを設定します
header("Content -type: text/html; charset=utf-8");//エンコーディングを設定します
次にフォーム情報を取得する必要があります
$ title = $_POST['title'];
$content = $_POST['content '];
$messtime = time();
次にメッセージを追加する必要があります
タイトルとコンテンツが入力されていない場合では、彼に提出させるべきではありません
if(empty($title)){
echo "<script>alert('タイトルを入力してください');history.go(-1);</script>" ; ('コンテンツを入力してください');history.go(-1);</script>";
}
それ以外の場合は、次のコードを追加できるはずです:
if(!empty($_SESSION[ 'name'])){
$ sql = "smess(title、content、messtime)values( '$ title'、 '$ content'、 '$ smesstime')"; ; アラート ('メッセージの追加に失敗しました'); & lt;/script & gt; " echo" & lt;ログイン後); 履歴 go(-1);</script>";
}
セッション ['name'] を判断します。空でない場合は、ログインしていることを意味します。メッセージを追加できます。それ以外の場合は、ログイン後にメッセージを残すように求められます
完全なコードは次のとおりです:
<?php
session_start();
header("Content-type: text/html; charset=utf-8");//设置编码
require_once('conn.php');
$title = $_POST['title'];
$content = $_POST['content'];
$messtime = time();
if(empty($title)){
echo "<script>alert('请输入标题');history.go(-1);</script>";
}elseif(empty($content)){
echo "<script>alert('请输入内容');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('请登录后添加留言');history.go(-1);</script>";
}
}
?>- おすすめコース
- コースウェアのダウンロード
現時点ではコースウェアはダウンロードできません。現在スタッフが整理中です。今後もこのコースにもっと注目してください〜 















