php實現簡易留言板效果

首先是Index頁面效果圖javascript

index.phpphp

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=[];
//檢測文件是否存在
if(file_exists($filename)){
  //讀取文件中的內容
  $string=file_get_contents($filename);
  if(strlen($string)>0){
    $msgs=unserialize($string);
  }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    簡易留言板-<span>V1.0</span>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    Hello, world!
                </h1>
                <p>
                    這是一個可視化佈局模板, 你能夠點擊模板裏的文字進行修改, 也能夠經過點擊彈出的編輯框進行富文本修改. 拖動區塊能實現排序.
                </p>
                <p>
                    <a rel="nofollow" class="btn btn-primary btn-large" href="#">參看更多 »</a>
                </p>
            </div>
             <?php if(is_array($msgs)&&count($msgs)>0):?>
            <table class="table">
                <thead>
                    <tr>
                        <th>
                            編號
                        </th>
                        <th>
                            用戶名
                        </th>
                        <th>
                            標題
                        </th>
                        <th>
                            時間
                        </th>
                        <th>
                            內容
                        </th>
                        <th>
                            操做
                        </th>
                    </tr>
                </thead>
                <tbody>
                <?php $i=1;foreach($msgs as $key=>$val):?>
              <tr class="success">
              <td>
                <?php echo $i++;?>
              </td>
              <td>
                <?php echo $val['username'];?>
              </td>
              <td>
                <?php echo $val['title'];?>
              </td>
              <td>
                <?php echo date("m/d/Y H:i:s",$val['time']);?>
              </td>
              <td>
                <?php echo $val['content'];?>
              </td>
              <td>
                <a href="edit.php?id=<?php echo $key;?>">編輯</a>|<a href="#" onclick="show_confirm(<?php echo $key;?>)">刪除</a>
              </td>
            </tr>
            <?php endforeach;?>
                </tbody>
            </table>
            <?php endif;?>
            <input type="button" class="btn btn-primary btn-lg" name="pubMsg" value="我要留言" onclick="window.location.href='add.php'"/>
        </div>
    </div>
</div>
<script type="text/javascript">
    function show_confirm(key){
        var r=confirm("肯定刪除嗎?");
        if (r==true){
              location.href='delete.php?id='+key;
        }
    }
    </script>
</body>
</html>

而後是新增留言頁面效果圖css

 

 

 

add.phphtml

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=[];
//檢測文件是否存在
if(file_exists($filename)){
  //讀取文件中的內容
  $string=file_get_contents($filename);
  if(strlen($string)>0){
    $msgs=unserialize($string);
  }
}
//檢測用戶是否點擊了提交按鈕
if(isset($_POST['addMsg'])){
  $username=$_POST['username'];
  $title=strip_tags($_POST['title']);
  $content=strip_tags($_POST['content']);
  $time=time();
  //將其組成關聯數組
  $data=compact('username','title','content','time');
  array_push($msgs,$data);
  $msgs=serialize($msgs);
  if(file_put_contents($filename,$msgs)){
    echo "<script>alert('留言成功!');location.href='index.php';</script>";
  }else{
    echo "<script>alert('留言失敗!');location.href='index.php';</script>";
  }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    簡易留言板-<span>V1.0</span>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    你終於來了!
                </h1>
                <p>
                    這是一個添加留言的留言板,你在下面愉快的留言吧!
                </p>
                <p>
                    <a rel="nofollow" class="btn btn-primary btn-large" href="#">參看更多 »</a>
                </p>
            </div>
            <form action="#" method="post">
                <fieldset>
                    <legend>發佈</legend>
                       <label>用戶名</label><input type="text" name="username" required />
                       <label>標題</label><input type="text" name="title" required />
                       <label>內容</label><textarea name="content" rows="5" cols="30" required></textarea>
                       <hr>
                       <input type="submit" class="btn btn-primary btn-lg" name="addMsg" value="發佈留言"/>
                       <input type="button" class="btn btn-primary btn-lg" value="查看留言" onclick="window.location.href='index.php'"/>
                </fieldset>
            </form>
        </div>
    </div>
</div>
</body>
</html>

編輯留言頁面java

edit.phpjquery

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$filename="msg.txt";
$msgs=[];
$id=$_GET['id'];//獲取id
//檢測文件是否存在
if(file_exists($filename)){
  //讀取文件中的內容
  $string=file_get_contents($filename);
  if(strlen($string)>0){
    $msgs=unserialize($string);
  }
}
//獲取已有的留言信息
$username=$msgs[$id]['username'];
$title=strip_tags($msgs[$id]['title']);
$content=strip_tags($msgs[$id]['content']);
$time=$msgs[$id]['time'];
//檢測用戶是否點擊了編輯按鈕
if(isset($_POST['editMsg'])){
  //將修改後的留言寫入文檔
  $msgs[$id]['username']=$_POST['username'];
  $msgs[$id]['title']=strip_tags($_POST['title']);
  $msgs[$id]['content']=strip_tags($_POST['content']);
  $msgs[$id]['time']=time();
  $msgs=serialize($msgs);
  if(file_put_contents($filename,$msgs)){
    echo "<script>alert('編輯成功!');location.href='index.php';</script>";
  }else{
    echo "<script>alert('編輯失敗!');location.href='index.php';</script>";
  }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-ui"></script>
<link href="http://www.francescomalagrino.com/BootstrapPageGenerator/3/css/bootstrap-combined.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row-fluid">
        <div class="span12">
            <div class="page-header">
                <h1>
                    簡易留言板-<span>V1.0</span>
                </h1>
            </div>
            <div class="hero-unit">
                <h1>
                    再來修改下~
                </h1>
                <p>
                    這是用來修改留言的地方哦!
                </p>
                <p>
                    <a rel="nofollow" class="btn btn-primary btn-large" href="#">參看更多 »</a>
                </p>
            </div>
            <form action="#" method="post">
                <fieldset>
                    <legend>編輯</legend>
                       <label>用戶名</label><input type="text" name="username" value="<?php echo $username;?>" required />
                       <label>標題</label><input type="text" name="title"  value="<?php echo $title;?>" required />
                       <label>內容</label><textarea name="content" rows="5" cols="30" required><?php echo $content;?></textarea>
                       <hr>
                       <input type="submit" class="btn btn-primary btn-lg" name="editMsg" value="編輯完成"/>
                       <input type="button" class="btn btn-primary btn-lg" value="查看留言" onclick="window.location.href='index.php'"/>
                </fieldset>
            </form>
        </div>
    </div>
</div>
</body>
</html>

此時儲存的留言信息:msg.txtbootstrap

a:2:{i:3;a:4:{s:8:"username";s:3:"cyy";s:5:"title";s:12:"cyy又來了";s:7:"content";s:27:"cyy常常來留言!!!";s:4:"time";i:1565510381;}i:4;a:4:{s:8:"username";s:3:"cyy";s:5:"title";s:17:"cyy2020第一踩~";s:7:"content";s:17:"cyy2020第一踩~";s:4:"time";i:1578723602;}}
相關文章
相關標籤/搜索