PHP(七)练习添加留言

添加留言页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>
    <h2>添加留言</h2>
    <form action="doaction.php" method="get">
        <input type="hidden" name="act" value="add"/>
        <table border="1" cellpadding="0" cellspacing="0" bgcolor="#3A8DFE">
            <tr>
                <td>留言者</td>
                <td><input type="text" name="userName" id="" placeholder="请输入您的昵称"></td>
            </tr>
            <tr>
                <td>标题</td>
                <td><input type="text" name="title" id="" placeholder="请输入您的标题"></td>
            </tr>
            <tr>
                <td>标题</td>
                <td><textarea name="content" id="" cols="45" rows="10"></textarea></td>
            </tr>
            <tr>
                <td>心情</td>
                <td>
                <input type="radio" name="xinqing" value="1.jpg" checked/><img src="img/1.jpg" width="100px" height="100px">
                <input type="radio" name="xinqing" value="2.jpg"/><img src="img/2.jpg" width="100px" height="100px">
                <input type="radio" name="xinqing" value="3.jpg"/><img src="img/3.jpg" width="100px" height="100px">
                <input type="radio" name="xinqing" value="4.jpg"/><img src="img/4.jpg" width="100px" height="100px">
                <input type="radio" name="xinqing" value="5.jpg"/><img src="img/5.jpg" width="100px" height="100px">
                </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit"></td>
            </tr>
        </table>
    </form>
</body>
</html>

获取留言页面

<?php
/**
 * 表单接收
 * 1、get 接收浏览器端数据 $_GET[参数名];
 * 2、post 接收post方式提交的数据 $_POST[参数名];
 * 3、request 接收post、get两种数据 $_REQUEST[参数名];
 */
$userName = isset($_GET['userName']) ? $_GET['userName'] : '';
$title = isset($_GET['title']) ? $_GET['title'] : '';
$content = isset($_GET['content']) ? $_GET['content'] : '';
$xinqing = isset($_GET['xinqing']) ? $_GET['xinqing'] : '';
$time = date('Y-m-d h:i:s');
$act = isset($_GET['act']) ? $_GET['act'] : '';
$filename = 'text.txt';
/*
 *1.判断有没有text.txt文件
 *2.判断文件里有没有数据
 *3.把text.txt数据取出来
 */
if (file_exists($filename)&&filesize($filename)>0) {
    //从text.txt里面取数据
    $str = file_get_contents($filename);
    //反序列化把字符转化成为二维数组
    $arr = unserialize($str);
}
if ($act=='add') {
    $arr[] = array(
        'userName' => $userName,
        'title' => $title,
        'content' => $content,
        'xinqing' => $xinqing,
        'time' => $time
    );
    //序列化数组
    $arr = serialize($arr);
    //存储数据
    if (file_put_contents($filename,$arr)){
        echo '添加留言成功<br/><a href="add.php">继续添加</a>|<a href="index.php">查看留言</a>';
    }else{
        echo '添加留言失败';
    }
}
?>

显示留言页面

<?php
    $filename = 'text.txt';
    if (file_exists($filename)&&filesize($filename)>0) {
        //从text.txt里面取数据
        $str = file_get_contents($filename);
        //反序列化把字符转化成为二维数组
        $userinfo = unserialize($str);
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>
    <h2>留言列表--<a href="add.php">添加留言</a></h2>
    <form action="doaction.php" method="get">
        <input type="hidden" name="act" value="add"/>
        <table border="1" cellpadding="0" cellspacing="0" bgcolor="#3A8DFE">
            <tr>
                <td>编号</td>
                <td>标题</td>
                <td>内容</td>
                <td>留言者</td>
                <td>发布时间</td>
                <td>心情</td>
            </tr>
            <?php
                foreach ($userinfo as $key => $val) {
            ?>
            <tr>
                <td><?php echo $key+1;?></td>
                <td><?php echo $val['title']?></td>
                <td><?php echo $val['content']?></td>
                <td><?php echo $val['userName']?></td>
                <td><?php echo $val['time']?></td>
                <td><img width="100px" height="100px" src="img/<?php echo $val['xinqing']?>"></td>
            </tr>
            <?php
                }
            ?>
        </table>
    </form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_38904347/article/details/83480267