Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
multiple forms on the same page, AJAX action on submit

I am looking to use AJAX to submit multiple forms on submit. I need to get the value of a hidden field to use in the AJAX call. I have the first submit working just fine and returns the response expected.

example:

<form name="add_to_cart-12345" action="add_product" method="post">
<input type="hidden" name="products_id" value="12345" />
<input class="quantity" id="product_quantity" min="1" name="cart_quantity" value="1" step="1" type="number"> 
<input type="submit" class="add-to-cart" value="Add to Cart" id="addProduct-12345" />

This code can repeat 1 to n times with the values 12345 changing and being unique.

My Javascript is:

<script>

$(function() { // begin document ready
$('#addProduct').on('click', function(e) {
    e.preventDefault();
    var cart_quantity = document.getElementById('product_quantity').value;
    var products_id = parseInt(document.getElementsByName('products_id').value);    

    var form = $(this).closest('form');
    // AJAX request
    $.ajax({
      url: 'ajax_add_product.php',
      type: 'POST',
      dataType : "html",
      data: {cart_quantity, products_id},
      success: function(response){
        
        console.log(response)
        var TotalInCart = parseInt(document.getElementById('cartTotal').innerText);
        var cartQuantity = parseInt(document.getElementById('product_quantity').value);         
        var headerTotal = cartQuantity + TotalInCart;
        $("#cartTotal").html(headerTotal);
        $("#cartProducts").html(response);
        $("#ShoppingCartModal").modal({show:true});
      },
      error: function(xhr, status, error) {
        alert(xhr.responseText);
      }
    });
 });
$( document ).ajaxError(function() {
  $( ".log" ).text( "Triggered ajaxError handler." );
});
 

});
</script>

What I need to do is to be able to submit any form when clicked. And push the product id, product quantity to the AJAX call.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda