I want to embed web2py forms in a static website (same domain) and keep the 
static part as simple as possible.

Currently I have this in the static website:
<div id="form"></div>
<script 
type="text/javascript">$("#form").load("path/to/web2py/app");</script>


And this in the web2py app:
def email():
    script = """
        $("#contact_form").submit(function(event) {
            $.post(this.action, $(this).serialize(), function(response) {
                $("#contact_form").replaceWith(response);
            });
            return false;
        });"""

    form = FORM(
        SCRIPT(script),
        TABLE(
            TR('Message:', TEXTAREA(_name='message', 
requires=IS_NOT_EMPTY())),
            TR('', INPUT(_type='submit')),
        ), 
        _action=URL(), 
        _id='contact_form'
    )
    if form.accepts(request):
        # process result ...
        return 'Message received'
    else:
        return form.xml()


The key part is: $("#contact_form").replaceWith(response);
Each AJAX request the form is replaced with the latest response, which is 
either a form with errors or the success message.

This works as expected, but is there a better way to do this? 

Richard

Reply via email to