How to reload a specific form after submit success using Ajax?

Ali Gajani

I am currently trying to reload a specific form, which is the form inside which the button was submitted, purely for the purpose of instant feedback. I am creating a Favorites button. This is my current setup. The ajax works but I still have to reload the page for it to show the new styles and even to change the METHOD type, which is required to unfavorite-favorite.

$.ajax({
    type: method,
    url: url,
    data: form.serialize(),
    success: function() {
        form.reload();
    }
});
Yoann

I guess you could clear all fields :

$.ajax({
    type: method,
    url: url,
    data: form.serialize(),
    success: function() {
        form.find('input:text, input:password, input:file, select, textarea').val('');
        form.find('input:radio, input:checkbox')
             .removeAttr('checked').removeAttr('selected');
    }
});

Taken from this answer.

EDIT :

As precised in comments, maybe you should get your server send back infos from the newly created item.

Then you could populate a "template" you have, and replace the form with it :

var newItem = $("<div class='newItem'></div>");

$.ajax({
    type: method,
    url: url,
    data: form.serialize(),
    success: function( data ) {
        //Get response from server like : {'name':'Name', 'attribut':'Attribut'}
        for( var key in data ) {
            newItem.append("<p>" + key + " : " + data[key] + "</p>");
        }
        form.replaceWith(newItem);
    }
});

An example JSFiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reload a page after successfully submit a form using ajax

From Dev

Reload a page after successfully submit a form using ajax

From Dev

Submit form only after ajax verification success

From Dev

how to reload a page using ajax success function

From Dev

Prevent page reload after form submit / node (no ajax available)

From Dev

JQuery Form No reload : how to pass the $_POST info after success?

From Dev

How to change url after success in ajax without page reload

From Dev

Submit form without reload using jQuery AJAX in PHP MySQL

From Dev

How to display success message in a form submit after sending a mail successfully using malsup form jquery without page refresh

From Dev

how to clear form after successful submit ajax

From Dev

How to Ajax POST after a Form Submit

From Dev

Issue with AJAX form submit success handler

From Dev

Ajax to submit form, and retrieve success/error

From Dev

Don't reload page after form submission using ajax and jquery

From Dev

Replacing HTML form with success message after submit, form sends mail using separate php file

From Dev

Form Submit button only works after reload

From Dev

Ajax reload shoping card after submit data

From Dev

How to load one div after another upon form submit using Ajax

From Dev

How to load one div after another upon form submit using Ajax

From Dev

How to show success message below html form after form submit which is being handled by different php file

From Dev

How to disable a submit button when AJAX request is in progress and enable it after receiving success AJAX response?

From Dev

Form submit after on ajax callback

From Dev

ajax form redirect after submit

From Dev

Ajax loading after form submit

From Dev

Ajax loading after form submit

From Dev

Form Submit after Ajax Callback

From Dev

Ajax show form after submit

From Dev

callback after ajax form submit rails using bootstrap

From Dev

Stay on same page after a form submit using ajax

Related Related

  1. 1

    Reload a page after successfully submit a form using ajax

  2. 2

    Reload a page after successfully submit a form using ajax

  3. 3

    Submit form only after ajax verification success

  4. 4

    how to reload a page using ajax success function

  5. 5

    Prevent page reload after form submit / node (no ajax available)

  6. 6

    JQuery Form No reload : how to pass the $_POST info after success?

  7. 7

    How to change url after success in ajax without page reload

  8. 8

    Submit form without reload using jQuery AJAX in PHP MySQL

  9. 9

    How to display success message in a form submit after sending a mail successfully using malsup form jquery without page refresh

  10. 10

    how to clear form after successful submit ajax

  11. 11

    How to Ajax POST after a Form Submit

  12. 12

    Issue with AJAX form submit success handler

  13. 13

    Ajax to submit form, and retrieve success/error

  14. 14

    Don't reload page after form submission using ajax and jquery

  15. 15

    Replacing HTML form with success message after submit, form sends mail using separate php file

  16. 16

    Form Submit button only works after reload

  17. 17

    Ajax reload shoping card after submit data

  18. 18

    How to load one div after another upon form submit using Ajax

  19. 19

    How to load one div after another upon form submit using Ajax

  20. 20

    How to show success message below html form after form submit which is being handled by different php file

  21. 21

    How to disable a submit button when AJAX request is in progress and enable it after receiving success AJAX response?

  22. 22

    Form submit after on ajax callback

  23. 23

    ajax form redirect after submit

  24. 24

    Ajax loading after form submit

  25. 25

    Ajax loading after form submit

  26. 26

    Form Submit after Ajax Callback

  27. 27

    Ajax show form after submit

  28. 28

    callback after ajax form submit rails using bootstrap

  29. 29

    Stay on same page after a form submit using ajax

HotTag

Archive