2-9作业编辑功能 提交没反应
<?php
header("Content-type:text/html;charset=utf-8;");
date_default_timezone_set("PRC");
$edkey=$_GET["edkey"];
$filename="msg.txt";
$msgs=[];
if(file_exists($filename)){
$string=file_get_contents($filename);
if(strlen($string)>0){
$msgs=unserialize($string);//如果有内容就把反序列化一下
}
$edarr=$msgs[$edkey];
if(!is_array($edarr) || count($edarr)<=0){
echo "<script>alert('编辑错误 ,无法编辑');location.href='index.php';</script>";
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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><span>编辑留言</span>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</h1>
<p>
这是一个可视化布局模板, 你可以点击模板里的文字进行修改, 也可以通过点击弹出的编辑框进行富文本修改. 拖动区块能实现排序.
</p>
</div>
<form action="index.php" method="post">
<fieldset>
<legend>发布留言</legend>
<label>用户名</label><input class="form-control" type="text" name="username" required value="<?php echo $edarr['username']?>"/>
<label>标题</label><input class="form-control" type="text" name="title" required value="<?php echo $edarr['title']?>"/>
<label>内容</label><textarea name="content" rows="6" cols="4" required value=""><?php echo $edarr['content']?></textarea>
<label></label><button type="submit" name="pubmsg" class="btn">编辑完成</button>
<a href="index.php"><button type="submit" class="btn">查看留言</button></a>
</fieldset>
<input type="hidden" name="edkey" id="" value="<?php echo $edkey ?>" />
</form>
</div>
</div>
</div>
</body>
</html><?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);//如果存在就读取这个文件内容 并且赋值给string
if(strlen($string)>0){
$msgs=unserialize($string);//如果有内容就把反序列化一下
}
}
if(isset($_POST["sub"])){
$username=$_POST["username"];
$title=strip_tags($_POST["title"]);//strip_tags()去除用户填写 html和php标记 注入
$content=strip_tags($_POST["content"]);
$time=time();
//将以上的post过来的数据组成关联数组;
$data=compact("username","title","content","time");
//把关联数组数据插入$msgs
array_push($msgs,$data);
//在把序列化一下,变成字符串 用于存储
$msgs=serialize($msgs);
//像filename 中写入 内容
if(file_put_contents($filename,$msgs)){
echo "<script>alert('留言成功');location.href='index.php'</script>";
}else{
echo "<script>alert('留言失败');location.href='index.php'</script>";
}
}
if(isset($_POST["pubmsg"])){
$username=$_POST["username"];
$title=strip_tags($_POST["title"]);//strip_tags()去除用户填写 html和php标记 注入
$content=strip_tags($_POST["content"]);
$time=time();
//将以上的post过来的数据组成关联数组;
$data=compact("username","title","content","time");
//把关联数组数据插入$msgs
//array_push($msgs,$data);
//替换数组的
//array_replace($msgs,$data);
//在把序列化一下,变成字符串 用于存储
$msgs=serialize($msgs);
//像filename 中写入 内容
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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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><span>留言板-</span>v1.0</small>
</h1>
</div>
<div class="hero-unit">
<h1>
Hello, world!
</h1>
<p>
这是一个可视化布局模板, 你可以点击模板里的文字进行修改, 也可以通过点击弹出的编辑框进行富文本修改. 拖动区块能实现排序.
</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("Y/m/d H:i:s",$val["time"]); ?>
</td>
<td>
<?php echo $val["content"]; ?>
</td>
<td>
<a href="edit.php?edkey=<?php echo $key ?>">编辑</a> |
<a href="delete.php?delekey=<?php echo $key ?>">删除</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<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="" cols="" required></textarea>
<label></label><button type="submit" name="sub" class="btn">提交</button>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>42
收起
正在回答 回答被采纳积分+1
3回答
(这张图片为您添加的留言)
(这张图片为您获取到的用户更改后编辑留言的内容)
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星