Label for attached to a radio input

1

I wish that when selecting the text of a radio button the corresponding button is assigned or selected. The nomenclature is usually that the name that we assign to label for must be the equivalent of the name of id of input .

But ... in a case automatically generated due to a query?

Muestro código:
//Si pulsamos el link "Eliminar opinión"...
if(isset($_GET["eliminar"])){
    //Llamamos al método "obtenerOpinionesPorCliente" y le pasamos el parámetro del email del cliente.
    $opiniones = BD::obtenerOpinionesPorCliente($cliente);
    if($opiniones != null){
        echo "<form action='menu_cliente.php?eliminar=1' name='form_a_eliminar' id='form_a_eliminar' method='POST'>";
        $selected = true;
        foreach ($opiniones as $opinion){
            $fecha = new DateTime($opinion["fecha"]);
            $comentario = $fecha->format('d/m/Y')."(".$opinion["idemail"].") - ".$opinion["opinion"]."<br/><br/>"; 
            echo "<input type='radio' name='a_eliminar' value='".$opinion["idopinion"]."' ".($selected?"checked":"").">".$comentario."</input>";
            //COMENTARIO!!! Que ID debo poner... para el label for...
            $selected = false;
        }
        echo "<input type='submit' name='eliminar_opinion' id='eliminar_opinion' value='Eliminar opinión'>";
        echo "</form>";
    }else{
        echo "<h1>¡No existen opiniones!</h1>";
        echo "<img src='imagenes/advertencia.png' height='160px' width='220px' alt='Advertencia'>";
    }
}
    
asked by omaza1990 04.01.2018 в 23:58
source

1 answer

0

It's simple. In this code segment:

$i=0; // defines un incremental para el id de tus radios
foreach ($opiniones as $opinion){
  $fecha = new DateTime($opinion["fecha"]);
  $comentario = $fecha->format('d/m/Y')."(".$opinion["idemail"].") - ".$opinion["opinion"]."<br/><br/>"; 
  echo "<input id='<?php echo $i; ?>' type='radio' name='a_eliminar' onclick='chequea(<?php echo $i; ?>)' style='cursor:pointer' value='".$opinion["idopinion"]."' ".($selected?"checked":"")."><span>".$comentario."</span>";

  $i++; // incrementas para que el proximo radio tenga un id diferente.
}

As you see in the previous code with the onclick event, you capture the event so that when you press the text of the radio button in question it is checked by the check () function which contains the following:

function chequea(radio){
    document.getElementById(radio).checked = true;
}

You can see the code working in the following snippet:

function chequea(radio){

document.getElementById(radio).checked = true;

}
<input id="1" type='radio' name='a_eliminar' value='esto'><span onClick="chequea(1)" style="cursor:pointer">Comentarios1</span><br>
<input id="2" type='radio' name='a_eliminar' value='esto'><span onClick="chequea(2)" style="cursor:pointer">Comentarioss</span><br>
    
answered by 05.01.2018 в 01:30