php简单的用户留言板实现内容(1-23)

静态模版来源

可视化布局

代码实现

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set("Asia/Shanghai");
/*
    构思:
        建立一个文本文件,通过操作文件,写入数据、读取数据!
        file_get_contents 读文件
        file_put_contents 写文件
        通过serialize 产生一个可储存的值
        通过unserialize 创建一个可储存的值
        通过读取值写入值,来保存操作我们需要的值
*/

// 文件名:
$filename =  'info.txt';

// 创建保存数据的数组
$msg = [];
/*
    [
        'username'=>'',
        'tit'=>'',
        'content'=>''
    ] 
*/


// 检测文件是否存在,若不存在,创建文件
if( !file_exists( $filename ) ){
    fopen($filename,'w+');
}
// 判断文件内是否有内容
$string = file_get_contents($filename);
if( strlen($string)>0 ){
    $msg = unserialize($string);
};

// 检测变量是否设置,是否点击提交内容了~
if ( isset( $_POST['pubMsg'] ) ){
    $username = $_POST['username'];
    $title = strip_tags($_POST['title']);
    $content = strip_tags($_POST['content']);
    // 时间'
    $time = time();
    $data = compact('username','title','content','time');
    // 添加到数组当中去
    array_push($msg,$data);
    $msg  = serialize($msg);
    // 写入数据
    if( file_put_contents($filename,$msg) ){
        echo "<script> alert('留言成功'); location.href = 'index.php';</script>";
    }else {
         echo "<script> alert('留言失败'); location.href = 'index.php';</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
    <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
</head>
<style>
    table tr:nth-child(even){
        background:#90EE90;
        
    }
</style>
<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!!!
                    </h1>
                    <p>
                        有事请留言
                    </p>
                    <p>
                        <a rel="nofollow" class="btn btn-primary btn-large" href="#">参看更多 »</a>
                    </p>
                </div>
                <!-- 
                    if(): endif;
                    判断是否为数组以及数组内数值是否为空
                -->
                <table class="table">
                <?php if ( is_array($msg) && count($msg)>0 ):?>
                    <thead>
                        <tr>
                            <th>
                                编号
                            </th>
                            <th>
                                用户名
                            </th>
                            <th>
                                标题
                            </th>
                            <th>
                                内容
                            </th>
                            <th>
                                时间
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr class="success">
                            <?php  $i=1; foreach ($msg as $value): ?>
                            <td>
                                <?php echo $i++ ?>
                            </td>
                            <td>
                                <?php echo $value['username']; ?>
                            </td>
                            <td>
                                <?php echo $value['title']; ?>
                            </td>
                            <td>
                                <?php echo $value['content']; ?>
                            </td>
                            <td>
                                <?php echo date('Y年-m月-d日-G时-i分',$value['time']); ?>
                            </td>
                        </tr>
                            <?php endforeach;?>
                        </tr>
                    </tbody>
                <?php endif;     ?>
                </table>
                <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="pubMsg" value="发布留言" />
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</body>

</html>

文件保存格式.php

转载于:https://www.cnblogs.com/ar13/p/8335117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值