自动保存表单输入使用原型和PHP [英] Autosaving Form Input Using Prototype and PHP

查看:150
本文介绍了自动保存表单输入使用原型和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取一个相对简单的自动保存系统,我想这样做,使用Prototype库。我使用的是PeriodicalUpdater请求,但它不工作,因为我所希望的。总之,我想,定期通过Ajax请求的PHP页面,将其保存到一个MySQL数据库发送一个文本区域的内容。我做这样的事情(简称code):

I'm implementing a relatively simple autosave system and I'd like to do so using the Prototype library. I'm using the PeriodicalUpdater request, but it's not working as I'd hoped. In short, I'm trying to, periodically, send a textarea's content via an AJAX request to a PHP page that will save it to a MySQL database. I'm doing something like (abbreviated code):

<html>

<head>

<script type="text/javascript" src="scripts/prototype.js"></script>

<script>

    function autosave() {
    	new Ajax.PeriodicalUpdater('save_message', 'autosave.php', 
    		{ 
    			method: 'post',
    			parameters: {id: $('id').value, save_text: $('myInput').value},
    		    frequency: 5,
    		    decay: 2

    		});
    }

</script>
</head>

<body>
<input type="hidden" id='id' name='id' />
<textarea id='myInput' name='myInput'></textarea>

<script>
autosave();
</script>
</body>
</html>

然后autosave.php将采取何种形式的内容,并将其写入到我的数据库。这部分工作正常。这是怎么回事,因为我发现,是PeriodicalUpdater被称为与原始表单输入,然后与最初的表单输入周期性调用。

Then autosave.php will take the form contents and write them to my database. That part is working fine. What is happening, as I discovered, is PeriodicalUpdater is called with the original form input, then is called periodically with that initial form input.

所以这是一个漫长的设置相对较短的问题:如何使用原型(如果可能)使用当前文本域的值,定期做一个AJAX请求

So that was a long setup for a relatively short question: How do I use Prototype (if possible) to periodically make an AJAX request using the current textarea's value?

推荐答案

你可以只使用的Ajax.Request在使用setInterval,是这样的:

you could just use Ajax.Request with setinterval,something like this:

document.observe("dom:loaded", function() { 
    intervalID = window.setInterval("autosave()",500);
});

function autosave() {
    new Ajax.Request('autosave.php', 
    { 
    	method: 'post',
    	parameters: {id: $('id').value, save_text: $('myInput').value},
    });
}

这篇关于自动保存表单输入使用原型和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆