How to get label by post with php?

0

Is there a way to receive the value of a label or other element that is next to an input by php? I have the following code.

<div class="formulario__category__plates">
  <?php $platos = mysqli_query($conexion, $pCombinations);
   while ($row = mysqli_fetch_assoc($platos)) { ?>
   <label class="formulario__plate__name"><?php echo $row ['name_plate'];?></label>
   <p class="formulario__plate__price"><?php echo number_format($row ['price_plate'], 2);?></p>
   **<input type="text" class="formulario__plate__input" value="0" name="combinations[]">**
   <input type="hidden" value="<?php echo $row ['price_plate'];?>">
 <?php } mysqli_free_result($platos);?>
</div>

I am sending some inputs as an array per post (which is in bold). And I already know how to receive them, but I also want to get the name and price of the input where some value has been written. But I have no idea how to do it. I hope you can help me. What I'm trying to do is something like this: link in the part where you put the number of dishes. Thanks.

    
asked by Jorge castillo 15.02.2018 в 21:18
source

2 answers

0

All the fields will have the same index, then you only need to make a foreach to access fields and values, but you should add the ID of each record, not the price:

<div class="formulario__category__plates">
<?php
    $platos = mysqli_query($conexion, $pCombinations);
    while ($row = mysqli_fetch_assoc($platos)) { ?>
<label class="formulario__plate__name"><?php echo $row ['name_plate'];?></label>
<p class="formulario__plate__price"><?php echo number_format($row ['price_plate'], 2);?></p>
<input type="text" class="formulario__plate__input" value="0" name="combinations[]">
<input type="hidden" name="id_plate[]" value="<?php echo $row ['id_plate'];?>">
<?php } mysqli_free_result($platos);?>
</div>

Processing the form:

<?php
foreach($_POST['id_plate'] as $i => $id_plate) {
    if($_POST['combinations'][$i] > 0) {
        // Aquí debes verificar que existe el producto
        // y también obtener el precio adecuado
        $result = mysqli_query($conexion, "SELECT * FROM platos WHERE id_plate = $id_plate");
        // Antes de la siguiente línea conviene revisar si
        // realmente existe el producto, creo que es con mysqli_num_rows()
        $plato = mysqli_fetch_assoc($result);
        // Ya puedes tomar el precio de $plato['price_plate']
    }
}

This code is just an example so you can access the combinations field and associate it with the product id, in addition to not allowing the user to modify things like the price, but, IMPORTANT you must avoid the SQL injection risk validating what you receive per form.

    
answered by 16.02.2018 / 05:21
source
0

Maybe there are better ways to do it, the first thing that has occurred to me is:

<div class="formulario__category__plates">
  <?php 
  $platos = mysqli_query($conexion, $pCombinations);
  $i = 0;
  while ($row = mysqli_fetch_assoc($platos)) {  
  ?>
  <label class="formulario__plate__name"><?php echo $row ['name_plate'];?></label>
  <p class="formulario__plate__price"><?php echo number_format($row ['price_plate'], 2);?></p>
 **<input type="text" class="formulario__plate__input" value="0" name="combinations[]">**
   <input type="hidden" name="nombre_<?php echo $i;?>" value="<?php echo $row ['name_plate'];?>">0
   <input type="hidden" name="precio_<?php echo $i;?>" value="<?php echo $row ['price_plate'];?>">
 <?php          
     $i++;
   } mysqli_free_result($platos);?>
   <input type="hidden" name="total" value="<?php echo $i?>">
</div>

you put a counter $ i, and the names of the input hidden, you add it to the end name="nombre_$i" and name="precio_$i" . You also add a hidden input with the total counter. Then in the php you pick it up with a for.

$total = filter_input(INPUT_POST, 'total') // que viene siendo $_GET['total'];
$array_con_todo = new Array();
for($i=0;$i<=$total;$i++){
  $array_con_todo[$i]['nombre'] = filter_input(INPUT_POST, 'nombre_'.$i);
  $array_con_todo[$i]['precio'] = filter_input(INPUT_POST, 'precio_'.$i);
}

and how you have the total of the inputs, because you walk one by one and you keep them.

This is the first way I could think of seeing your code. Surely there is something more effective

    
answered by 15.02.2018 в 21:45