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

101
Vistas
la llamada ajax devuelve solo la última identificación

Estoy tratando de desarrollar una función de agregar al carrito a través de ajax, el problema es que no importa qué producto agregue al carrito, agrega el último artículo en la lista y luego incrementa el mismo producto al hacer clic en cualquier otro producto. Aquí está mi código: -

 <?php $i = 1; while($row = mysqli_fetch_array($run_products)){ $op_id = $row['option_id']; echo "<tr>"; echo "<td>" . $i . "</td>"; echo "<td>" . $row['option_desc'] . "</td>"; echo "<td> <form action='' method='post' class='p_form'> <input type='text' name='product_id' value='$op_id' pid='$op_id'> <input type='text' name='quantity' value='1' pid='$op_id'> <input class='btn btn-primary add' type='submit' name='add_to_cart' value='Add to Cart' id='product' pid='$op_id'> </form> </td>"; echo "</tr>"; $i++; } ?>

Aquí está jquery:

 <script type="text/javascript"> $(document).ready(function(){ $(document).on('click','.p_form',function (event) { event.preventDefault(); var element = $(this); var id = element.attr("pid"); //alert(id); $.ajax({ url: 'action.php', method: 'post', //data: {id:id}, data:$('.p_form').serialize(), success: function(data){ $('#message').html(data); } }); return false; }); }); </script>

Aquí está action.php , $product_id siempre regresa como 4 (por ejemplo) si la última identificación en la última es 4

 if (!empty($_POST)){ $product_id = $_POST['product_id']; echo $product_id; }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Necesito hacerlo como a continuación: -

 <script type="text/javascript"> $(document).ready(function(){ $(document).on('click','.add',function (event) {//instead of form click add event on form button click event.preventDefault(); var element = $(this); var id = element.attr("pid"); //alert(id); $.ajax({ url: 'action.php', method: 'post', data:element.parent('.p_form').serialize(), //serialize clicked button parent form success: function(data){ $('#message').html(data); } }); return false; }); }); </script>
over 4 years ago · Santiago Trujillo Denunciar

0

Desencadenar el evento de clic en el botón, no en el clic del formulario:

Esto funcionará para usted para cada producto.

 <script type="text/javascript"> $(document).ready(function(){ $(document).on('click','.add',function (event) { event.preventDefault(); var element = $(this); var id = element.attr("pid"); //alert(id); $.ajax({ url: 'action.php', method: 'post', data: {id:id}, //send id of product you wish to add success: function(data){ $('#message').html(data); } }); return false; }); }); </script>

Y del lado del servidor:

 if (!empty($_POST)){ $product_id = $_POST['id'];//get only that id posted in ajax call echo $product_id; }
over 4 years ago · Santiago Trujillo Denunciar
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