On 25 November 2010 10:06, Rasmus Skaarup <ml...@gal.dk> wrote:

>
> Is is possible to submit a form, but stay on the page without reloading it?
>
> Currently I redirect to the same page the user came from after the values
> are saved to the database, which doesn't mess up any values, but forces a
> reload on the user and places him on the top of the page. If thats not
> possible, then does anybody know if it's possible to tell which html anchor
> the user is closest to when submitting (either pressing return, or using the
> button)?
>

You can use jquery's ajax forms plugin to do this very easily:

http://jquery.malsup.com/form/

Or more manually, send the form data from the submit event. Example from
http://api.jquery.com/jQuery.post/:

    $.post("test.php", $("#testform").serialize());

The ajax plugin does the same thing more or less, but checks the form's
action URI, HTTP method, etc automatically.

The submission results are available in a callback either way.


As for actually getting the submit event, you could do this in one of two
styles in your form definitions. The first option is to do it per form:

    javascript: |
        $("form:last").submit(function(form) { /* ajax post here */; return
false });

$("form:last") selects the last form element in the document, which is
currently being defined (just the form container, no elements yet IIRC due
to where formfu injects the <script> element).

This is appropriate if you only need this for one special form.

A more generic way is to do:

    attributes:
        class: ajaxy

And in your .js files somewhere:

    $(document).ready(function() {
        $("form.ajaxy").submit(function (form) { ...; return false });
    });



Finally, since the 'submit' handler uses 'return false' to override normal
form submission, you can keep your redirect logic in case the user has
disabled javascript, and consider this value-added ajax (i.e. you don't
actually need to depend on the JS for functionality, it just makes it a bit
sleeker).
_______________________________________________
HTML-FormFu mailing list
HTML-FormFu@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/html-formfu

Reply via email to