weixin_33698823 2011-07-27 21:22 采纳率: 0%
浏览 15

为什么我的Ajax AutoSave无法正常工作?

I'm using a script found on this page: http://jetlogs.org/2007/11/11/auto-saving-with-jquery/ to autosave a form I have. I'm only trying to save the textarea for the form. Here's the relevant code:

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

And here's the 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';

?>

It seems almost as though the ajax isn't even working, since I'm not seeing any display changes or error messages on the page where the form is located.

Any ideas? Thanks!

  • 写回答

3条回答 默认 最新

  • 游.程 2011-07-27 21:26
    关注

    your textarea has an id of elm1

    <textarea id="elm1" name="comments" rows="15" cols="80" style="width: 80%">
    

    but your trying to access it by

    $("#comments")
    

    change to

    $("#elm1")
    

    or

    $("[name=comments]")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3