質問

Is it possible to submit multiple forms with a single 'submit' button? If so, what's the way to do this? Thanks.

正しい解決策はありません

他のヒント

This is strictly speaking, not a web2py question but a HTML (DOM - document object model) and manipulating DOM with javascript question.

You can submit 2 (or more forms) in one go, clicking on one single button, by using javascript.

Like this:-

<form id="id1" ...> ...</form>
<form id="id2" ...> ...</form>

<button onclick="document.getElementById('id1').submit();document.getElementById('id2').submit();" />

You will probably run into problems when you have to requests POSTed to two separate urls/functions in your web2py application which now needs to be handled concurrently?

The right approach is probably that you will probably need to write a function that changes your form objects into fieldsets as part of one single form.

In django, formsets does exactly this and converts multiple forms into fieldsets belonging to only 1 single HTML form in the template so what was original "multiple forms" become part of 1 single form in the frontend.

Like this:-

<form method="post" action="">
    {{ formset.management_form }}
    {% for form in formset %}
        {{ form.id }}
        <ul>
            <li>{{ form.title }}</li>
            {% if formset.can_delete %}
                <li>{{ form.DELETE }}</li>
            {% endif %}
        </ul>
    {% endfor %}
</form>

web2py:-

http://www.web2pyslices.com/slice/show/1458/two-forms-on-one-page-with-custom-forms-to-update-record

def new():
    form = SQLFORM.factory(db.contact,db.groups)

    if form.accepts(request.vars):
        _id_user = db.contacts.insert(**db.contacts._filter_fields(form.vars))

        form.vars.contact = _id_user

        id = db.groups.insert(**db.groups._filter_fields(form.vars))

        response.flash = 'User registered successfully'
    return locals()

which returns to your web2py template 1 single form only.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top