I wrote a similar Baidu space automatically save the draft program Php+jquery

Source: Internet
Author: User

Can asynchronously load drafts from MySQL ~, update draft list asynchronously ~

: http://download.csdn.net/source/3479156

Code:

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 (' Draft saved successfully! '); History.back ( -1);</script> ";

}else{

echo "<script>alert (' draft save failed! '); History.back ( -1);</script> ";

}

}else{

echo "<script>alert (' title and content cannot be empty!) '); History.back ( -1);</script> ";

}

}

?>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>

<title>php+jquery automatically save drafts </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>

<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;" >

Draft box (<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" >

Title: <input name= "title" type= "text" id= "title" Size= "/>"

<br/><br/>

Content: <textarea name= "Content" id= "content" cols= "rows=" ></textarea>

<br/><br/>

<input name= "Submit" type= "Submit" value= "Save Draft"/>

<input name= "paper_id" id= "paper_id" type= "hidden" value= "<?php Echo (Time () +28800). Rand (0,10000);? > "/>

</form>

<table id= "Data" width= "" "border=" 1 "cellspacing=" 0 "cellpadding=" 0 "style=" border-collapse:collapse "; Text-align:center; " >

<tr>

<th colspan= "2" > Drafts in draft box </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= "Load draft"/><input name= "Load" type= "hidden" value= "< ? php echo $info [' id '];? > "/></td>

</tr>

<?php

}

?>

</table>

<table id= "format" style= "Display:none;" >

<tr>

<td>&nbsp;</td>

<td><input name= "Load_paper" type= "button" value= "Load draft"/><input name= "Load" type= "hidden" value= ""/ ></td>

</tr>

</table>

</div>

</body>

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." Automatically save drafts successfully! ")," numb "= ($numb + 1)," id "and" $id "," title "and" $title "," tag "=>1);

}else{

$returnmsg =array ("Time" = "$timestamp." Auto Save draft failed! ")," numb "= $numb," id "and" "," title "and" "";

}

}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." Automatically save drafts successfully! ")," Numb "and" $numb, "" id "+ $info [" id "]," title "and" $title, "" tag "=>2);

}else{

$returnmsg =array ("Time" = "$timestamp." Auto Save draft failed! ")," numb "= $numb," id "and" "," title "and" "";

}

}else{

$returnmsg =array ("Time" and "" "," Numb "and" "", "id" = "", "title" and "" ");

}

}

echo Json_encode ($returnmsg);

}

?>

News.sql

--PhpMyAdmin SQL Dump

--Version 3.1.5-rc1

--Http://www.phpmyadmin.net

--

--Host: localhost

--Date of generation: October 02, 2010 06:20

--Server version: 5.0.18

--PHP version: 5.2.8

SET sql_mode= "No_auto_value_on_zero";

/*!40101 SET @[email protected] @CHARACTER_SET_CLIENT */;

/*!40101 SET @[email protected] @CHARACTER_SET_RESULTS */;

/*!40101 SET @[email protected] @COLLATION_CONNECTION */;

/*!40101 SET NAMES UTF8 */;

--

--Database: ' Test '

--

-- --------------------------------------------------------

--

--The structure of the table ' News '

--

CREATE TABLE IF not EXISTS ' news ' (

' id ' int (one) unsigned not NULL auto_increment,

' paper_id ' varchar (not NULL),

' title ' varchar (not NULL),

' content ' varchar (not NULL),

PRIMARY KEY (' id ')

) Engine=innodb DEFAULT charset=gb2312 auto_increment=1;

--

--Export the data in the table ' News '

--

I wrote a similar Baidu space automatically save the draft program Php+jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.