I have the following code in PHP and HTML:
In the form where I receive the data:
<form autocomplete="off" action="insertar.php" method="post">
foreach($data["bajos"] as $dato){
$cal_id = $dato["cal_id"];
$pco_nombre = $dato["pco_nombre"];
$pas_nombre = $dato["pas_nombre"];
?> <p class="card-text"><strong><?php echo $pco_nombre; ?></strong></p>
<p class="card-text"><?php echo $pas_nombre; ?></p>
<p class="card-text">
<div style="float:right;">
<label>Votacion:</label>
<input type="number" name="cal_id" id="<?php echo $cal_id;?>" value="<?php echo $cal_id;?>">
<input type="number" name="votacion[]" id="votacion"></input>
</div> <?php
}
?>
<button type="submit" id="submit3" name="boton1" class="btn btn-secondary" value="Guardar" type="button">Consultar</button>
</form>
And it looks like this inside the page:
In the code that I attached above there are 2 input numbers, within which 1 is already fed with an ID.
The action code is this:
if(isset($_POST['votacion'])){
$votos = $_POST['votacion']; //Se reciben los datos de el input donde digita el usuario
}
if(isset($_POST['cal_id'])){
$cal_id = $_POST['cal_id']; //Se reciben los datos que ya estaban alimentados anteriormente para el QUERY
}
sort($votos); //Organizo de menor a mayor el array de Votos
$voto = max($votos); //Tomo el valor del array mayor para insertarlo en el QUERY
echo "Se insertara el valor: ".$voto." y su ID es: ".$cal_id."";
But when you see the result, it shows the following:
And with any number you put it is always the same ID
Is there something you're doing wrong? Thank you very much in advance!
On the server side, as you already know, the information of a form submitted with POST is received in the $_POST array-dictionary. The keys of this array are the name attributes of each form field. If you notice, in your loop you generate several inputs, but they all have the same name="cal_id", so in the end the form only sends a single value, usually the last one (in some browsers it could be the first one).
update
You will have to give each input a different name or
name = "cal_id[]"
. By having [] in the name, all field values will be sent in an array. So $cal_id will be an array with all the values. So, $cal_id[0] would give you the value of the first input (in your image I think it's 203, unless there is one above).By the way, if you already use max you don't need to sort the array of votes. And conversely, if you sort, you don't need max (the largest will be last, if you sort from smallest to largest).
If instead of using max or sort you walk through a classic for looking for the maximum, it will be easy for you to identify the index where it is located. This way you can use the same index on $cal_id to get the corresponding id.
Code:
How does it work
array_keys returns the keys of the array (in a conventional array, its index), but also supports a search element. If we give it the largest of the values, it will give us all the positions that contain that value. Since we are only interested in one, we obtain the first one, in case there are duplicates with [0].
Once we have the position (index) of the largest, we can use it in both arrays to obtain the value (the vote) and its id.