does not the alert of the following variables show me only shows the first selected data?

0
<html>
<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<?php 

$user="localhost";
$username="root";
$pass="";
$db="surti_office";

$conexion=mysqli_connect($user,$username,$pass,$db)or die  ("ERROR DE CONEXION");
?>

<?php 
$consultar="SELECT * FROM producto where total_oferta_iva >1 ORDER by idproducto desc limit 20";
$resultado=$conexion->query($consultar);
while ($resultados=$resultado->fetch_assoc()){
?>
<form action="#" id="formulario">

<input type="text" id="input" name="<?php echo $resultados['idproducto']?>" value="<?php echo $resultados['idproducto']?>">
<input type="submit" value="Enviar" />
</form>
<?php }?>

<script type="text/javascript">
$(document).ready(function(){
    $("#formulario").submit(function(){
        var cadena="";
        cadena = $(this).serialize();
     alert(cadena);
     return false;
     });
     });
</script>

    
asked by Leonardo Mancero 31.10.2017 в 16:24
source

1 answer

0

If you want to generate multiple forms then you should do the following:

You have an error in your structure html and that is that all the forms that are generated you are assigning the same id you should never repeat the id of an element within your page to that then we will use classes in the following way:

<?php 
$consultar="SELECT * FROM producto where total_oferta_iva >1 ORDER by idproducto desc limit 20";
$resultado=$conexion->query($consultar);
while ($resultados=$resultado->fetch_assoc()){
?>
<form action="#" class="formulario">

<input type="text" id="input" name="<?php echo $resultados['idproducto']?>" value="<?php echo $resultados['idproducto']?>">
<input type="submit" value="Enviar" />
</form>
<?php }?>

<script type="text/javascript">
$(document).ready(function(){
    $(".formulario").submit(function(){
        var cadena="";
        cadena = $(this).serialize();
     alert(cadena);
     return false;
     });
     });
</script>

What happened in your case was that since they all had the same id the system only interacted with the first one you found ignoring the rest.

    
answered by 31.10.2017 / 16:28
source