On the google App Engine, I would like to use javascript (or Ajax) to POST a form and then update the target div. The form contains a lot of fields and files to transfer. The javascript function is copied from the "Javascript: The Definite Guide" book. I have 2 questions:
- In "form.html", how do I prepare the "data" argument for the postFormData() function so that I can pass all the fields and files.
- How to design the callback function, so that the response (i.e., "form.html") may update the content div?
Thanks for your help.
base.html:
...
<div id="content">
{% include "form.html" %}
</div>
Image:<br />
<img src="/file?entity_id={{entity.key}}" />
<br />
<script type="text/javascript">
function postFormData(url, data, callback) {
if (typeof FormData === "undefined")
throw new Error("FormData is not implemented");
var request = new XMLHttpRequest();
request.open("POST", url);
request.onreadystatechange = function() {
if (request.readystate === 4 && callback)
callback(request);
};
var formdata = new FormData();
for (var name in data) {
if (!data.hasOwnProperty(name)) continue;
var value = data[name];
if (typeof value === "function") continue;
formdata.append(name, value);
}
request.send(formdata);
}
</script>
...
form.html
<form action="/form" method="POST" enctype="multipart/form-data">
name1: <input type="text" name="name" />{{ name1 }}<br /><br />
name2: <input type="text" name="name" />{{ name }}<br /><br />
...
file1: <input type="file" name="file" />{{ file1 }}<br /><br />
file2: <input type="file" name="file" />{{ file2 }}<br /><br />
...
<input type="submit" value="Submit" onclick="return postFormData('/form', howToPrepareData?, whatIsTheCallbackFunction?)" />
</form>