2016-03-24 62 views
0

我有下面的代码,当用户空闲时,它应该自动提交(保存)名为“project”的表单。这是我在教程网站上找到的代码(忘记名称),我试过了,它只刷新页面?空闲时自动保存表格

<!-- Set auto save timeout in milliseconds 
<script type="text/javascript"> 
attachEvent(window,'load',function(){ 
    var idleSeconds = 5; 
    var idleTimer; 
    function resetTimer(){ 
    clearTimeout(idleTimer); 
    idleTimer = setTimeout(whenUserIdle,idleSeconds*1000); 
    } 
    attachEvent(document.body,'mousemove',resetTimer); 
    attachEvent(document.body,'keydown',resetTimer); 
    attachEvent(document.body,'click',resetTimer); 

    resetTimer(); // Start the timer when the page loads 
}); 

function whenUserIdle(){ 
     document.project.submit(); 
     window.location = location.href; 
} 

function attachEvent(obj,evt,fnc,useCapture){ 
    if (obj.addEventListener){ 
    obj.addEventListener(evt,fnc,!!useCapture); 
    return true; 
    } else if (obj.attachEvent){ 
    return obj.attachEvent("on"+evt,fnc); 
    } 
} 
</script> --> 

表格代号:

<form name="project" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="invoice-form" method="post" class="invoice-form" role="form" novalidate> 

回答

1

这是刷新页面window.location = location.href;尝试取出它的代码。

而且您还需要确保您的表单的属性name正在替换document.project.submit();中的“项目”。

例如

<form name="test_form"></form> 

document.test_form.submit(); 


编辑:
好了,该功能应该只是

function whenUserIdle() { 
    document.project.submit(); 
} 
+0

我将添加我的表单代码 – Steven

+0

我知道它刷新,它应该是,它只是没有提交它应该的形式 – Steven

+0

这是因为你在提交之前重新加载页面,你需要在重新加载页面之前发送数据 –

0

,而不是做一个实际的表单提交,可考虑做一个AJAX请求:

function whenUserIdle(){ 
    var formData = {}; // assemble the form data here 
    $.post("/form-submission-route", formData, function(responseData) { 
    // do something with result, if you like 
    // perhaps clear the form or throw up a notification 
    }); 
} 
+0

我完全不熟悉Ajax。上面的代码确实具有一定的常识,但“组装数据在这里”部分如何处理样本数据除外? – Steven