自动保存文本区域每隔多少秒 [英] Auto-save textarea every so many seconds

查看:116
本文介绍了自动保存文本区域每隔多少秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动保存一个textarea帮助。基本上,只要用户键入的文本区域,我想保存在我们的数据库中的草稿。因此,例如,一个用户键入一个博客帖子。每15秒,我想的脚本来更新与已输入到文本区域中的所有文本输入到数据库中。

我想这要完成通jQuery的/阿贾克斯,但我似乎无法找到任何东西,是满足我的需求。

在这个问题上的任何帮助是极大的AP preciated!

更新:

下面是我的PHP code:

 < PHP

$ Q = $ _ GET [Q];
$答案= $ _ GET [A];

//连接到数据库
require_once('mysql_connect.php');

$ SQL =UPDATE english_backup集合Q$ Q=$回答。WHERE STUDENT_ID = {$ _COOKIE ['学生']} LIMIT 1。
$结果= mysqli_query($ DBC,$ SQL);
 

?>

下面是我的javascript code:

 <脚本类型=文/ JavaScript的>
功能showUser(STR,答案)
{
如果(STR ==)
  {
  的document.getElementById(txtHint)的innerHTML =。
  返回;
  }
如果(window.XMLHtt prequest)
  {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
  XMLHTTP =新XMLHtt prequest();
  }
其他
  {// code对IE6,IE5
  XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
  }
xmlhttp.onreadystatechange =功能()
  {
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
    {
    的document.getElementById(txtHint)的innerHTML = xmlhttp.responseText。
    }
  }
xmlhttp.open(GET,getuser_english.php Q =?+ STR +&放大器; A =+答案,真正的);
xmlhttp.send();
}
< / SCRIPT>
 

解决方案

我想你想要像阿贾克斯...我用ajax jQuery的,所以你需要的 jQuery库的为它工作。下载它。你可以找到该网站的文档选项卡上的教程。

  //在你的JavaScript文件或部分
$(textarea的#myTextArea)。绑定(的keydown,函数(){
        myAjaxFunction(THIS.VALUE)//一样myAjaxFunction($(textarea的#myTextArea)。VAL())
});

功能myAjaxFunction(值){
    $阿贾克斯({
        网址:yoururl.php
        键入:POST,
        数据:textareaname =+值,
        成功:功能(数据){
            如果(!数据){
                警报(无法保存文件!);
            }
        }
    });
}

 //在你的PHP的一部分
 $文字= $ _ POST [textareaname];
 // $ user_id是人打字的ID
 $ SQL =更新草案设置文本='$文字。在哪里user_ID的='$ USER_ID。'; //尝试使用其它类型的查询不喜欢这个。这仅是一个例子
 的mysql_query($ SQL);
 如果(mysql_affected_rows()函数== 1){
    真正的呼应;
 }其他回声假;
 

嘿大家,我仍然有问题,请在这里看到的<一个href="http://stackoverflow.com/questions/10050785/php-parse-html-using-querypath-to-plain-html-characters-like-facebook-twitter">http://stackoverflow.com/questions/10050785/php-parse-html-using-querypath-to-plain-html-characters-like-facebook-twitter

I need help with "auto-saving" a textarea. Basically, whenever a user is typing in the textarea, I would like to save a "draft" in our database. So for example, a user is typing a blog post. Every 15 seconds I would like for the script to update the database with all text input that was typed into the textarea.

I would like for this to be accomplished thru jQuery/Ajax but I cannot seem to finding anything that is meeting my needs.

Any help on this matter is greatly appreciated!

UPDATE:

Here is my PHP code:

<?php

$q=$_GET["q"];
$answer=$_GET["a"];

//Connect to the database
require_once('mysql_connect.php') ;

$sql="UPDATE english_backup SET q".$q."='".$answer."' WHERE student_id = {$_COOKIE['student']} LIMIT 1";
$result = mysqli_query($dbc, $sql);

?>

Here is my javascript code:

<script type="text/javascript">
function showUser(str, answer)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser_english.php?q="+str+"&a="+answer,true);
xmlhttp.send();
}
</script>

解决方案

I think you want something like ajax... I'm using ajax jQuery so you will need jQuery Library for it to work. Download it. You can find tutorials on the documentation tab of the website.

 //in your javascript file or part
$("textarea#myTextArea").bind("keydown", function() {
        myAjaxFunction(this.value) //the same as myAjaxFunction($("textarea#myTextArea").val())
});

function myAjaxFunction(value) {
    $.ajax({
        url: "yoururl.php",
        type: "POST",
        data: "textareaname=" + value,
        success: function(data) {
            if (!data) {
                alert("unable to save file!");
            }
        }
    });
}

 //in your php part
 $text = $_POST["textareaname"]; 
 //$user_id is the id of the person typing
 $sql = "UPDATE draft set text='".$text."' where user_id='".$user_id."'"; //Try another type of query not like this. This is only an example
 mysql_query($sql);
 if (mysql_affected_rows()==1) {
    echo true;
 } else echo false;

Hey everyone I'm still having a problem please see here http://stackoverflow.com/questions/10050785/php-parse-html-using-querypath-to-plain-html-characters-like-facebook-twitter

这篇关于自动保存文本区域每隔多少秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆