I have for some time been working with the ajax based forms so I don't
have a full redrawn on submitting a form.
I also tend to have multiple buttons on my forms, as well as multiple
TABs that might have submit buttons with the same value.
(In a FORM submitted in the normal way, one would add a name="submit"
to the submit button to have the value of the button posted)
The default form.serialize() does not include the submit buttons
value, nor the name of the submit button, so I changed this a bit to
get the both the name as well as the value of the submit button:
function web2py_trap_form(action,target) {
jQuery('#'+target+' form').each(function(i){
var form=jQuery(this);
jQuery(':submit,.submit',this).click(function(){
var button = jQuery(this);
if (button.hasClass("deletebutton")) {
var objstr = button.attr("alt");
var delstr1 = "Are you ";
var delstr2 = "sure you want to delete " + objstr;
var delextra = "really ";
if(!confirm(delstr1 + delstr2))
return false;
if(!confirm(delstr1 + delextra + delstr2))
return false;
};
jQuery('.flash').hide().html('');
var name = button.attr('name');
if (name === '') name = 'submit';
if (name !== '')
name = '&' + name + '=' + button.attr('value').replace(' ',
'%20');
/* append submit information */
var serial = form.serialize() + name;
form.html('loading ...');
web2py_ajax_page('post',action,serial,target);
return false;
});
});
}
The ' if (button.hasClass("deletebutton")) ' works on submit buttons
that have the class deletebutton assigned to them. It prompts Are you
sure you want to delete XXX, with XXX taken from that alt= value of
the deletebutton. Then it prompts once more with 'Are you really...'
As the retrieval from the server might take a few moments the form
value is first replaced by the word 'loading...'
I always add the submit button even if it has no name= defined. If
that is not what you want remove the
if (name === '') name = 'submit';
line.
It took a while to get this figured out and working correctly so maybe
this is of help to someone.