0

テキストエリアの「自動保存」について助けが必要です。基本的に、ユーザーがテキストエリアに入力するたびに、データベースに「下書き」を保存したいと思います。たとえば、ユーザーがブログ投稿を入力しているとします。15 秒ごとに、テキストエリアに入力されたすべてのテキスト入力でスクリプトがデータベースを更新するようにします。

これを jQuery/Ajax で実現したいのですが、自分のニーズを満たすものを見つけることができないようです。

この問題に関するヘルプは大歓迎です!

アップデート:

ここに私のPHPコードがあります:

<?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);

?>

ここに私のJavaScriptコードがあります:

<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>
4

2 に答える 2

5
function saveText() {
    var text = $("#myTextArea").val();
    // ajax call to save the text variable

    // call this function again in 15 seconds
    setTimeout(saveText, 15000);
}();
于 2012-04-07T01:33:30.857 に答える
1

ajaxのようなものが欲しいと思います...私はajax jQueryを使用しているので 、それを機能させるにはjQueryライブラリが必要です。ダウンロードしてください。チュートリアルは、Web サイトのドキュメント タブにあります。

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

みなさん、まだ問題があります。こちらをご覧ください https://stackoverflow.com/questions/10050785/php-parse-html-using-querypath-to-plain-html-characters-like-facebook-twitter

于 2012-04-07T01:47:14.273 に答える