How to update "form / form" without Refresh?

2

My form is a shopping bag, when .submit () the form # add-cart-head, it adds the product without refresh, but I wanted to have an answer that updates the bag that sits on top of page. The "GET" in the sequence even works, but does not update form.

What to do, or how to do?

jQuery(document).ready(function() {
    jQuery('#add-to-cart-head').submit(function() {
        var $this = jQuery(this),
            dados = $this.serialize();
        	jQuery.ajax({
            type: "POST",
            url: $this.attr('action'),
            data: dados,
			complete: function(){
			jQuery.ajax({
				type: "GET",
				url: "https://modernita.ambienteprotegido.com/cart/update_cart",
				data: dados});
			},
			error: function(){
                alert("Deu erro");
            }
    });
	return false;
});
});
    
asked by anonymous 06.12.2014 / 01:31

1 answer

3

You already have almost everything, what is missing is:

  • know which element to update
  • know where the content you want to insert into this element comes from

If for example you want to add new content to a div you can use $('#idDaMinhaDiv').html('O carrinho foi atualizado!'); . If the content that is displayed is static then the example above is what you need. If the content comes from the AJAX then it has to pass on the data that comes from the server that information.

You have two AJAX calls, you can join a complete function as in the first one and then insert the data into the div. Something like:

jQuery.ajax({
    type: "POST",
    url: $this.attr('action'),
    data: dados,
    complete: function () {
        jQuery.ajax({
            type: "GET",
            url: "https://modernita.ambienteprotegido.com/cart/update_cart",
            data: dados,
            complete: function (mensagem) {
                $('#idDaMinhaDiv').html(mensagem);
            }
        });
    },
    error: function () {
        alert("Deu erro");
    }
});
    
06.12.2014 / 09:50