我正在使用此页面上的脚本:http://jetlogs.org/2007/11/11/auto-saving-with-jquery/自动保存我拥有的表单。我只是想为表单保存textarea。这是相关的代码:
<head>
<script type="text/javascript">
$(document).ready(function(){
autosave();
});
function autosave()
{
var t = setTimeout("autosave()", 5000);
var comments = $("#comments").val();
if (comments.length > 0)
{
$.ajax(
{
type: "POST",
url: "autosave.php",
data: "rubric_id=" + <?php echo $rubricid ?> + "&student_id=" + <?php echo $studentid ?> + "&comments=" + comments,
cache: false,
success: function(message)
{
$("#autosave_status").empty().append(message);
}
});
}
}
</script>
</head>
<body>
<div id="autosave_status"></div>
<form action='assess.php?student=146&rubric=19' method='POST'>
<textarea id="elm1" name="comments" rows="15" cols="80" style="width: 80%">
</form>
</body>
这是PHP:
<?php
//include DB configuration file
include "../../signout/database.php";
$comments = mysql_real_escape_string($_POST['comments']);
$rubric_id = (int)$_POST['rubric_id'];
$student_id = (int)$_POST['student_id'];
//save contents to database
$sql = "UPDATE rubrics_comments SET comments = '$comments' WHERE studentid = '$student_id' AND rubricid='$rubric_id'";
mysql_query($sql) or die (mysql_error());
echo $sql;
//output timestamp
echo 'Saved';
?>
似乎ajax甚至没有工作,因为我没有在表单所在的页面上看到任何显示更改或错误消息。
有什么想法吗?谢谢!
答案 0 :(得分:5)
你的textarea的id为elm1
<textarea id="elm1" name="comments" rows="15" cols="80" style="width: 80%">
但您尝试通过
访问它$("#comments")
更改为
$("#elm1")
或
$("[name=comments]")
答案 1 :(得分:1)
您的textarea的ID不是“评论”。并且您正在使用ID选择器$("#id");
答案 2 :(得分:1)
两件事:
更改
var comments = $("#comments").val();
到
var comments = $("#elm1").val();
关闭textarea元素:
</textarea>