Hello friends, I have a question, I have the following code, I explain in the php I do the traversal of the array that is variable, its size shows everything well but in the onclick event of the button the first value of the route stored in the input is always sent, When I click on the other buttons, it always gives me the first value. I don't know where the problem is in the js or php.
PHP CODE
<script type="text/javascript" src ="script.js"></script>
<?php
$datos1 = $array;
$longitudx = count($datos1);
for($i=0; $i<$longitudx; $i++){
?>
<?php echo $datos1[$i]['numeroPartida']?>
<input id="numpartida" name="numpartida" type="text" value="<?php echo $datos1[$i]['numeroPartida'] ?>">
<input type="button" class="btn btn-success" name="enviar" value="Detalle" href="javascript:;"
onclick="DetallePatida($('#numpartida').val());">
<?php
}
?>
<div id="resultado"></div>
JS CODE
function DetallePatida(numpartida) {
var parametros = {"numpartida":numpartida};
$.ajax({
data:parametros,
url:'resultado.php',
type: 'post',
beforeSend: function () {
$("#resultado").html("Procesando, espere por favor");
},
success: function (response) {
$("#resultado").html(response);
}
});
}
The problem you have is that you are not dynamically setting the IDs of the inputs. By having them inside a loop, you end up with duplicate IDs (
id="numpartida"
) and the IDs have to be unique.