2-12留言板 点击提交后不出现表格
来源:2-12 留言板功能实现
阿噜
2017-11-26 11:57:48
<?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['pubMsg'])){
$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='liuyanban.php';<script>";
}else{
echo "<script>alert('留言失败!');location.href='liuyanban.php';<script>";
}
}
//
/*
$msgs=[
[...],
[...]
]
file_get_contents($filename)得到文件中的内容,返回的是字符串
file_put_contents($filename)向指定文件写内容,如果文件不存在汇创建
Serialize($str)序列化字符串
unSerialize($str)
*/
print_r($msgs);
?>
<!DOCTYPE html>
<html>
<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>
页面标题实例<small>页面副标题</small>
</h1>
</div>
<dl>
<dt>
Rolex
</dt>
<dd>
劳力士创始人为汉斯.威尔斯多夫,1908年他在瑞士将劳力士注册为商标。
</dd>
<dt>
Vacheron Constantin
</dt>
<dd>
始创于1775年的江诗丹顿已有250年历史,
</dd>
<dd>
是世界上历史最悠久、延续时间最长的名表之一。
</dd>
<dt>
IWC
</dt>
<dd>
创立于1868年的万国表有“机械表专家”之称。
</dd>
<dt>
Cartier
</dt>
<dd>
卡地亚拥有150多年历史,是法国珠宝金银首饰的制造名家。
</dd>
</dl>
<?php if(is_array($msgs)&&count($msgs)>0):?>
<table class="table">
<thead>
<tr>
<th>
编号
</th>
<th>
用户名
</th>
<th>
标题
</th>
<th>
时间
</th>
<th>
内容
</th>
</tr>
</thead>
<tbody>
<?php $i=1;foreach($msgs as $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>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php endif;?>
<form action='#' method="post">
<fieldset>
<legand>请留言</legand>
<label>用户名</label><input type="text" name="username" required/>
<label>标题</label><input type="text" name="title" required/>
<label>内容</label><textarea name="content" row="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>
2回答
阿噜
提问者
2017-11-30
action为空,代表什么呢
好帮手慕查理
2017-11-27
您好,1.检测文件中的内容时格式错误,应为if(strlen($string)>0){}。
2.form提交用户内容时,action为空。
3.script标签应有结束标签,不能都为开始标签。
<?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['pubMsg'])){
$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='liuyanban.php';</script>";
}else{
echo "<script>alert('留言失败!');location.href='liuyanban.php';</script>";
}
}
//
/*
$msgs=[
[...],
[...]
]
file_get_contents($filename)得到文件中的内容,返回的是字符串
file_put_contents($filename)向指定文件写内容,如果文件不存在汇创建
Serialize($str)序列化字符串
unSerialize($str)
*/
print_r($msgs);
?>
<!DOCTYPE html>
<html>
<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>
页面标题实例<small>页面副标题</small>
</h1>
</div>
<dl>
<dt>
Rolex
</dt>
<dd>
劳力士创始人为汉斯.威尔斯多夫,1908年他在瑞士将劳力士注册为商标。
</dd>
<dt>
Vacheron Constantin
</dt>
<dd>
始创于1775年的江诗丹顿已有250年历史,
</dd>
<dd>
是世界上历史最悠久、延续时间最长的名表之一。
</dd>
<dt>
IWC
</dt>
<dd>
创立于1868年的万国表有“机械表专家”之称。
</dd>
<dt>
Cartier
</dt>
<dd>
卡地亚拥有150多年历史,是法国珠宝金银首饰的制造名家。
</dd>
</dl>
<?php if(is_array($msgs)&&count($msgs)>0):?>
<table class="table">
<thead>
<tr>
<th>
编号
</th>
<th>
用户名
</th>
<th>
标题
</th>
<th>
时间
</th>
<th>
内容
</th>
</tr>
</thead>
<tbody>
<?php $i=1;foreach($msgs as $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>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php endif;?>
<form action='' method="post">
<fieldset>
<legand>请留言</legand>
<label>用户名</label><input type="text" name="username" required/>
<label>标题</label><input type="text" name="title" required/>
<label>内容</label><textarea name="content" row="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>如果解决了您的问题,请采纳,祝学习愉快!
相似问题