简体   繁体   English

创建文档时自动过帐

[英]Automatically POST on document creation

In an HTML-document, i want to have a (if possible: invisible) form that POSTs the contents of one text input field to my server as soon as the document is created or if possible within s seconds. 在HTML文档中,我希望具有一种(如果可能的话:不可见的)表单,该表单将在文档创建后或在几秒钟内将一个文本输入字段的内容发布到我的服务器上。 The document is created via javascript's open. 该文档是通过javascript的open创建的。

How can i do that? 我怎样才能做到这一点?

No matter what you are trying to do :) you can submit the form any time like this with javascript: 无论您要做什么,您都可以使用javascript随时提交表单:

document.form_name.submit();

Let's say you want to submit the form as soon as page loads, you can do like: 假设您要在页面加载后立即提交表单,您可以执行以下操作:

<head>
<script type="text/javascript">
window.onload = function(){
  document.form_name.submit();
};
</script>
</head>

You may want to use some of the AJAX magic here. 您可能要在这里使用一些AJAX魔术。

You can achieve this by by using a POST request for getting the client communicate some data to the server. 您可以通过使用POST请求来使客户端与服务器通信一些数据来实现此目的。

I recommend reading this jQuery AJAX Tutorial . 我建议阅读此jQuery AJAX教程

The XMLHttpRequest object (the object responsible for nifty AJAX things like Google maps and Gmail) allows for invisible communication with your server from the browser. XMLHttpRequest对象(负责处理漂亮的AJAX之类的对象,例如Google地图和Gmail)允许通过浏览器与服务器进行不可见的通信。 I recommend using some AJAX library to cover the cross-browser differences. 我建议使用一些AJAX库来解决跨浏览器的差异。 THe most popluar one is Jquery, which allows code like this: 最流行的一种是Jquery,它允许这样的代码:

$.post('/path/to/script.php', {
  "key": "Value",
  "anotherkey": "it's value"
}, function (dat){
  //this function will be called when it is succesful
  //the variable dat is populated witht he server's response.
});

This allows for simple seamless communication with the browser. 这允许与浏览器的简单无缝​​通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM