views:

37

answers:

3

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. The document is created via javascript's open.

How can i do that?

+1  A: 

No matter what you are trying to do :) you can submit the form any time like this with 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>
Web Logic
where do i put the "window.onload = ... " code?If i place the code in the current window, it should be like:self.onload = ...or?
ptikobj
@ptikobj: You should put it in `head` tags, see my updated answer.
Web Logic
thanks, your tip suffices, since right now I don't need any additional features so that it might make sense to dive into AJAX.
ptikobj
okay, now another thing: what do i have to do if i only want to run this code once, i.e. if it should only be run the first time the new document is created. since i use django, the document is reloaded several times.
ptikobj
@ptikobj: you can use the session or cookie for that to determine if the form was redirected, don't do again.
Web Logic
A: 

You may want to use some of the AJAX magic here.

You can achieve this by by using a POST request for getting the client communicate some data to the server.

I recommend reading this jQuery AJAX Tutorial.

Arnaud Leymet
A: 

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. I recommend using some AJAX library to cover the cross-browser differences. THe most popluar one is Jquery, which allows code like this:

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

Rixius