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:
How to design the callback function, so that the response (i.e., "form.html") may update the content div?
如何设计回调函数,以便响应(即“form.html”)可以更新内容div?
Thanks for your help.
谢谢你的帮助。
base.html:
base.html文件:
...
{% include "form.html" %}
Image:
...
form.html
form.html
name1: {{ name1 }}
name2: {{ name }}
...
file1: {{ file1 }}
file2: {{ file2 }}
...
3 个解决方案
#1
1
When you do a Form POST (user clicks submit button or called via JS) then browser will reload the window and display the result of the POST. This is obviously not what you want.
$(document).ready(function()
{
$('.submit').on('click', function()
{
if($(".name").val()==""){
$(".name").val("Enter Name Here")
}
else {
var s = $(".name").val();
var n = $(".imageform").attr("action");
$(".imageform").attr("action", function() {
return s+n;
}
$("#callback").html('Uploading.....');
$(".imageform").ajaxForm(
{
target: '#callback'
}).submit();
}
});
});
also include this link in head:
还包括这个链接:
hey man all the text which will echo in php file will be shown in #callback. So, if you want to preview image please echo there html and do not remove that action=/form/index.php?name= and also in php file type $name=$_GET['name'];
嘿,所有将在php文件中回显的文本将显示在#callback中。所以,如果你想要预览图像,请回显那里的html,不要删除那个动作= / form / index.php?name =和php文件类型$ name = $ _ GET ['name'];
#3
0
I came up with the following code. But, strangely, it worked sometimes, other times it didn't. When it failed, the error message displayed in Firebug was "ReferenceError: $ is not defined". Can somebody tell me what the problem is and if this is a correct solution? Thanks.
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function(returnData) {
$('#content').html(returnData)
});
});