Php+ajax article Auto-Save Code instance method _php Tutorial

Source: Internet
Author: User
Php+ajax article Auto-Save Code instance method The main is to facilitate users, improve user experience Oh, we are using AJAX to save the data a temporary data, like CSDN, he can automatically save the user's data, so that is the power-off Oh, the unexpected you edit the data are not lost.

This is part of the core of AutoSave drafts,
Autosavetime (SEC) This function is used to start the timing.
Cleartimeout (Autosavetimer); Clear timer
document.getElementById (' Autosavetimebox '). innerhtml=sec+ "seconds"; Get the Autosavetimebox in the page and write a countdown to it

if (sec>0) {
Autosavetimer = SetTimeout ("Autosavetime (" +sec+ "-1)", 1000);
This is the case when Sec>0, the first second to execute autosavetime this function, while the value of Sec-1 will be written to Autosavetimebox
}else {
var Title=document.getelementbyid (' title ');
if (title.value!= ') {
Autosave_post ();
}else{
document.getElementById (' Autosavetimebox '). Innerhtml= ' Do not save ';
}
This part, is when the sec>0 condition is not tenable, hehe, is sec<=0 time, starts to carry on saves the draft
The first is to determine whether the title of the article is empty, if it is not empty, execute autosave_post () This function, or write ' Do not save '
}
PHP Tutorial Code

var useragent = Navigator.userAgent.toLowerCase ();
var Is_opera = (Useragent.indexof (' opera ')! =-1);
var Is_saf = ((Useragent.indexof (' AppleWebKit ')! =-1) | | (Navigator.vendor = = ' Apple computer, Inc. ');
var IS_WEBTV = (Useragent.indexof (' WebTV ')! =-1);
var Is_ie = ((Useragent.indexof (' msie ')! =-1) && (!is_opera) && (!IS_SAF) && (!IS_WEBTV));
var is_ie4 = ((Is_ie) && (Useragent.indexof (' MSIE 4. ')! =-1));
var Is_moz = ((navigator.product = = ' Gecko ') && (!IS_SAF));
var Is_kon = (useragent.indexof (' Konqueror ')! =-1);
var Is_ns = ((Useragent.indexof (' compatible ') = =-1) && (Useragent.indexof (' Mozilla ')! =-1) && (!is_oper A) && (!IS_WEBTV) && (!IS_SAF));
var is_ns4 = ((Is_ns) && (parseint (navigator.appversion) = = 4));
var Is_mac = (Useragent.indexof (' mac ')! =-1);
if ((Is_ie &!is_ie4) | | is_moz | | is_saf | | is_opera)
{
var allowajax=1;
}else{
var allowajax=0;
}
var xmlHttp = false;
function Makesenddata (postdata,url,functionname,httptype) {

var Posturl=url;
try {
XmlHttp = new ActiveXObject ("Msxml2.xmlhttp");
} catch (e) {
try {
XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
} catch (E2) {
XmlHttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest! = ' undefined ') {
XmlHttp = new XMLHttpRequest ();
}

if (!xmlhttp) {
Alert (' Cannot send an XMLHTTP request ');
return false;
}

How to submit a form
Xmlhttp.open (Httptype, PostURL, true);

Triggers an event when a form submission is complete
var changefunc= "xmlhttp.onreadystatechange =" +functionname; From Bob
eval (changefunc);
Xmlhttp.setrequestheader (' Content-type ', ' application/x-www-form-urlencoded ');
Xmlhttp.send (PostData);
}
function Autosave_post ()
{
var Title=document.getelementbyid (' title '). Value;
var content = window.frames["Editor"].window.frames["HtmlEditor"].document.getelementsbytagname ("BODY") [0]. InnerHTML;
var Posttime=document.getelementbyid (' Posttime '). Value;
if (allowajax==1)
{
Content=postencode (content);
Title=postencode (title);
var post= "title=" +title+ "&content=" +content+ "&posttime=" +posttime+ "";
var url= "Ajax.php?act=autosave";
Makesenddata (Post,url, ' AutoSave ', ' post ');
}
}
function AutoSave ()
{
if (xmlhttp.readystate = = 4)
{
if (Xmlhttp.status = = 200)
{
var Autoresponse=xmlhttp.responsetext;
var Automessage=document.getelementbyid (' Autosavetimebox ');
if (Autoresponse.indexof ("")!=-1)
{
Automessage.innerhtml= ' You have not yet written a message, do not save the draft ';
return false;
}
if (Autoresponse.indexof ("")!=-1)
{
Automessage.innerhtml= ' Save is successful, you can load the draft in the event of an accident.
Finddraft ();
}
}
}
}
function Finddraft ()
{
if (allowajax==1)
{
var url= "Ajax.php?act=loaddraft";
Makesenddata (Null,url, ' loaddraft ', ' POST ');
}
}
function Loaddraft ()
{
var Draftbox=document.getelementbyid (' draft ');
if (Xmlhttp.readystate < 4)
{
Draftbox.innerhtml= ' draft loaded in ... ';
}
if (xmlhttp.readystate = = 4)
{
if (Xmlhttp.status = = 200)
{
Draftbox.innerhtml=xmlhttp.responsetext;
}
}
}
function Cleardraft ()
{
if (allowajax==1)
{
var url= "Ajax.php?act=cleardraft";
Makesenddata (Null,url, ' nodraft ', ' POST ');
}
}
function Nodraft ()
{
var Draftbox=document.getelementbyid (' draft ');
if (Xmlhttp.readystate < 4)
{
Draftbox.innerhtml= ' Gta5-in ... ';
}
if (xmlhttp.readystate = = 4)
{
if (Xmlhttp.status = = 200)
{
Draftbox.innerhtml=xmlhttp.responsetext;
}
}
}
Encode string
function Postencode (str) {
Str=encodeuricomponent (str);
if (Is_moz) str=str.replace (/%0a/g, "%0d%0a"); From Bob
return str;
}

Auto-Save JS Code

var Autosavetimer;
Function Autosavetime (sec) {
Cleartimeout (Autosavetimer);
document.getElementById (' Autosavetimebox '). innerhtml=sec+ "seconds";
if (sec>0) {
Autosavetimer = SetTimeout ("Autosavetime (" +sec+ "-1)", 1000);
}else {
var Blogtitle=document.getelementbyid (' title ');
if (blogtitle.value!= ') {
Autosave_post ();
}else{
document.getElementById (' Autosavetimebox '). Innerhtml= ' Do not save ';
}
}
}
function Startimer ()
{
var Starttime=document.getelementbyid (' Autosavetimebox '). InnerHTML;
if (starttime== ' Save is successful, you can load a draft in case of an accident ' | | starttime== ' You have not written a message, do not save the draft ')
{
Starttime= ' 60 ';
}else{
Starttime=starttime.replace (' seconds ', ');
}
var Autosavefunbox=document.getelementbyid (' Autosavefunbox ');
Autosavefunbox.innerhtml= ' Stop timing ';
Starttime==0? Starttime=60:starttime=starttime;
Autosavetime (starttime);
}
function Stoptimer ()
{
var Autosavefunbox=document.getelementbyid (' Autosavefunbox ');
Autosavefunbox.innerhtml= ' Start timing ';
Cleartimeout (Autosavetimer);
}

Php+ajax article Auto-Save Code instance method The main is to facilitate users, improve user experience Oh, we are using AJAX to save the data a temporary data, like CSDN, he can automatically save the user's data, so that is the power-off Oh, the unexpected you edit the data are not lost.


http://www.bkjia.com/PHPjc/444937.html www.bkjia.com true http://www.bkjia.com/PHPjc/444937.html techarticle php+ajax article Auto-Save Code instance method The main is to facilitate users, improve the user experience oh, we are using AJAX to save data a temporary data, like CSDN, he can automatically save ...

  • 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.