Comments (3)

yes maybe I also took a different approach
in web2py_ajax
in the top add
response.files.insert(3,URL(r=request,c='static/js',f='jquery.form.js'))
you can get this plugin at http://jquery.malsup.com/form/
in the bottom replace with the following
function web2py_trap_form(action,target) {
jQuery('#'+target+' form').each(function(i){
var form=jQuery(this);
if(!form.hasClass('no_trap')){
if(form.find('.upload').length>0){
//using ajaxForm has the disadvantage that the header is not returned in xhr
//can this be fixed in the ajaxForm plugin???
form.ajaxForm({
url: action,
success: function(data, statusText, xhr) {
complete_web2py_ajax_page(xhr, data, action, target)
}
});
}else{
form.submit(function(obj){
jQuery('.flash').hide().html('');
web2py_ajax_page('post',action,form.serialize(),target);
return false;
});
}
}
});
}
function complete_web2py_ajax_page (xhr, text, action, target){
var html=xhr.responseText;
var content=xhr.getResponseHeader('web2py-component-content');
var command=xhr.getResponseHeader('web2py-component-command');
var flash=xhr.getResponseHeader('web2py-component-flash');
var t = jQuery('#'+target);
if(content=='prepend') t.prepend(html);
else if(content=='append') t.append(html);
else if(content!='hide') t.html(html);
web2py_trap_form(action,target);
web2py_ajax_init();
if(command) eval(command);
if(flash) jQuery('.flash').html(flash).slideDown();
}
function web2py_ajax_page(method,action,data,target) {
jQuery.ajax({'type':method,'url':action,'data':data,
'beforeSend':function(xhr) {
xhr.setRequestHeader('web2py-component-location',document.location);
xhr.setRequestHeader('web2py-component-element',target);},
'complete':function(xhr,text){
complete_web2py_ajax_page(xhr, text, action, target);
}
});
}
function web2py_component(action,target) {
//jQuery(document).ready(function(){ //i had to uncomment this since it cause some trouble???
$('#'+target).prepend('{{=IMG(_alt="loading ...", _src=URL(request.application, 'static/images', 'loading.gif'))}}');//just some eycandy, you can remove this if you want
web2py_ajax_page('get',action,null,target);
// });
}
Now if you have a form in a ajax loaded page and the form contains an upload field it will use the form plugin. Be aware that this will not allow you to send js in the header if plugin is used.