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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀