<?php
session_start();
include "../conn/config.php";
//验证是否是自己留言
if($_SESSION['user_id'] == $_SESSION['con_id']){
	echo '<script type="text/javascript">alert("您不能给自己留言！");window.close();</script>';
}

if($_POST['sub']){
	$title = trim($_POST['title']);
	$content = trim($_POST['content']);
	$userid = $_SESSION['user_id'];
	$receiveid = $_SESSION['con_id'];
	$date = date("Y-m-d H:i:s");
	
	if($title == ""){
		echo "<script type='text/javascript'>alert('标题不能为空！');history.back();</script>";
	}
	
	if(strlen($title) > 20){
		echo "<script type='text/javascript'>alert('标题字数不能超过10！');history.back();</script>";
    }
	
	if($content == ""){
		echo "<script type='text/javascript'>alert('内容不能为空！');history.back();</script>";
	}
	
	if(strlen($content) > 600){
		echo "<script type='text/javascript'>alert('内容字数不能超过300！');history.back();</script>";
	}
	
	$query = mysql_query("select username from {$total_pr}user where id='".$userid."'");
	 while($row = mysql_fetch_assoc($query)){
	      $user = $row['username'];	
	 }
	 
	 $query = mysql_query("insert into {$total_pr}le_mess (title,content,sendid,sendname,receiveid,status,date) values ('$title','$content','$userid','$user','$receiveid','0','$date')");
	 
	 if($query){
		 echo "<script type='text/javascript'>alert('留言发表成功！');window.close();</script>";
     }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../css/le_mess.css" />
<title>博客留言板</title>
<script type="text/javascript">
   function check(con){
	   if(con.title.value == ""){
		   alert("标题不能为空！");
		   con.title.focus();
		   return false;
	   }
	   
	   if(con.title.value.length > 10){
		   alert("标题字数不能超过10！");
		   con.title.focus();
		   return false;
	   }
	   
	   if(con.content.value == ""){
		   alert("内容不能为空！");
		   con.content.focus();
		   return false;
	   }
	   
	   if(con.content.value.length > 300){
		   alert("内容字数不能超过300！");
		   return false;
	   }
	   
	   return true;
   }
</script>
</head>

<body>
  <div id="le_mess">
     <h3>留言板</h3>
     <form action="" method="post"  onsubmit="return check(this)">
       <div class="t">
         标题：<input type="text" class="title" name="title" />
         <span class="pro">标题内容不得超过10字</span>
       </div>
     <textarea name="content" class="content" /></textarea>
     <div class="button">
       <input type="submit" value="提交留言" name="sub" class="sub" />
       <span class="pro">内容不得超过300字</span>
       <input type="reset" value="重写留言" name="re" class="re" />
     </div>
     </form>
  </div>
</body>
</html>
