可以异步加载mysql中的草稿~,异步更新草稿列表~
下载地址:
代码:
demo.php
<?php
mysql_connect("localhost","root","root");
mysql_select_db("test");
mysql_query("SET CHARACTER SET GB2312");
if($_POST["Submit"]){
$title=trim($_POST["title"]);
$content=trim($_POST["content"]);
$paper_id=$_POST["paper_id"];
if($title!="" and $content!=""){
$query="select `paper_id` from `news` where `paper_id`='$paper_id'";
$result=mysql_query($query);
$info=mysql_fetch_array($result);
if($info==""){
$query="insert into `news` (paper_id,title,content) values ('$paper_id','$title','$content')";
}else{
$query="update `news` set `title`='$title',`content`='$content' where `paper_id`='$info[paper_id]'";
}
if(mysql_query($query)){
echo "<script>alert('草稿保存成功!');history.back(-1);</script>";
}else{
echo "<script>alert('草稿保存失败!');history.back(-1);</script>";
}
}else{
echo "<script>alert('标题和内容均不能为空!');history.back(-1);</script>";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php+jquery自动保存草稿</title>
<style>
body{
font-size:12px;
margin:0 auto;
line-height:20px;
}
</style>
<script language="JavaScript" type="text/javascript" src="jquery.js"></script>
<script>
function save_paper(){
var titleval=$("#title").val();
var contentval=$("#content").val();
var paper_idval=$("#paper_id").val();
var numbval=$("#numb span").text();
titleval=$.trim(titleval);
contentval=$.trim(contentval);
if(titleval!="" && contentval!=""){
$.post(
'save_paper.php',
{
title:titleval,
content:contentval,
paper_id:paper_idval,
numb:numbval
},
function(data){
var returnmsg='';
eval('returnmsg='+data+";");
if(returnmsg.time!="" && returnmsg.numb!=""){
$("#returnmsg").html(returnmsg.time);
$("#numb span").html(returnmsg.numb);
if(returnmsg.id!="" && returnmsg.title!="" && returnmsg.tag!=""){
if(returnmsg.tag==1){
$("#format tr td:eq(0)").text(returnmsg.title);
$("#format tr td:eq(1) input:eq(1)").val(returnmsg.id);
$("#data tr:last").after($("#format tr").clone(true));
}else if(returnmsg.tag==2){
$("#data tr td input[value='"+returnmsg.id+"']").parent().prev().text(returnmsg.title);
}
}
}
}
);
}
}
function load_paper(obj){
var load_idval=obj.next().val();
if(load_idval!=""){
$.post(
'load_paper.php',
{
id:load_idval
},
function(data){
var info='';
eval('info='+data+";");
$("#title").val(info.title);
$("#content").val(info.content);
$("#paper_id").val(info.paper_id);
}
);
}
}
$(document).ready(function(){
setInterval("save_paper();",15000);
$("input[name='load_paper']").click(function(){
load_paper($(this));
});
});
</script>
</head>
<body>
<div align="center">
<div id="returnmsg" style="border:1px solid #0099FF;background:#00CC33;width:400px;height:20px;"></div>
<div id="numb" style="border:1px solid #0099FF;background:#0099FF;width:400px;height:20px;">
草稿箱(<span style="font-weight:bold">
<?php
$query="select COUNT(`id`) as `numb` from `news`";
$result=mysql_query($query);
$info=mysql_fetch_array($result);
echo $info["numb"];
?>
</span>)
</div>
<br /><br />
<form action="" method="post" name="form1">
标题:<input name="title" type="text" id="title" size="100" />
<br /><br />
内容:<textarea name="content" id="content" cols="100" rows="10"></textarea>
<br /><br />
<input name="Submit" type="submit" value="保存草稿" />
<input name="paper_id" id="paper_id" type="hidden" value="<?php echo (time()+28800).rand(0,10000);?>" />
</form>
<hr />
<table id="data" width="600" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse;text-align:center;">
<tr>
<th colspan="2">草稿箱中的草稿</th>
</tr>
<?php
$query="select * from `news`";
$result=mysql_query($query);
while($info=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $info["title"];?></td>
<td><input name="load_paper" type="button" value="加载草稿" /><input name="load" type="hidden" value="<?php echo $info['id'];?>" /></td>
</tr>
<?php
}
?>
</table>
<table id="format" style="display:none;">
<tr>
<td> </td>
<td><input name="load_paper" type="button" value="加载草稿" /><input name="load" type="hidden" value="" /></td>
</tr>
</table>
</div>
</body>
</html>
load_paper.php
<?php
mysql_connect("localhost","root","root");
mysql_select_db("test");
mysql_query("SET CHARACTER SET UTF8");
$id=trim($_POST["id"]);
if($id!=""){
$query="select * from `news` where `id`='$id'";
$result=mysql_query($query);
$info=mysql_fetch_array($result);
echo json_encode($info);
}
?>
save_paper.php
<?php
mysql_connect("localhost","root","root");
mysql_select_db("test");
mysql_query("SET CHARACTER SET UTF8");
$title=trim($_POST["title"]);
$content=trim($_POST["content"]);
$paper_id=trim($_POST["paper_id"]);
$numb=trim($_POST["numb"]);
if($title!="" and $content!=""){
$query="select * from `news` where `paper_id`='$paper_id'";
$result=mysql_query($query);
$info=mysql_fetch_array($result);
$timestamp=date("Y-m-d H:i:s",time()+28800);
if($info==""){
$query="insert into `news` (paper_id,title,content) values ('$paper_id','$title','$content')";
if(mysql_query($query)){
$id=mysql_insert_id();
$returnmsg=array("time"=>($timestamp."自动保存草稿成功!"),"numb"=>($numb+1),"id"=>$id,"title"=>$title,"tag"=>1);
}else{
$returnmsg=array("time"=>($timestamp."自动保存草稿失败!"),"numb"=>$numb,"id"=>"","title"=>"");
}
}else{
if($info['title']!=$title or $info["content"]!=$content){
$query="update `news` set `title`='$title',`content`='$content' where `paper_id`='$paper_id'";
if(mysql_query($query)){
$returnmsg=array("time"=>($timestamp."自动保存草稿成功!"),"numb"=>$numb,"id"=>$info["id"],"title"=>$title,"tag"=>2);
}else{
$returnmsg=array("time"=>($timestamp."自动保存草稿失败!"),"numb"=>$numb,"id"=>"","title"=>"");
}
}else{
$returnmsg=array("time"=>"","numb"=>"","id"=>"","title"=>"");
}
}
echo json_encode($returnmsg);
}
?>
news.sql
-- phpMyAdmin SQL Dump
-- version 3.1.5-rc1
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 10 月 02 日 06:20
-- 服务器版本: 5.0.18
-- PHP 版本: 5.2.8
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `news`
--
CREATE TABLE IF NOT EXISTS `news` (
`id` int(11) unsigned NOT NULL auto_increment,
`paper_id` varchar(15) NOT NULL,
`title` varchar(256) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 AUTO_INCREMENT=1 ;
--
-- 导出表中的数据 `news`
--