How to display the radio input selected by a user when printing the screen

1

is the first time I ask for help since I'm stuck in a code for a few days, I looked for tutorials and help online but I could not solve them, so I go to bother them a little while to see if someone can give me a hand.

I'm doing a form in html which then by php shows me the data entered by the user, but my big doubt comes when using the inputs of radio type or checkbox. It wants to show on the screen the data that was saved by printing the item selected by the user but at the same time I also see the other selected options. So far what I could do is show that selected data but duplicating the option. Next I put the code that I am doing outside the project to see if I understand the dynamics. Thank you very much for the help you can give me. Regards!

    <form action="pruebaRB.php" name="formPruebaJs" method="post">

<tr>      
<td style="text-align: center;">
  <input type="radio" name="nombreEmpleado" value="juan" id="nombreEmpleado"> Juan
  <input type="radio" name="nombreEmpleado" value="carlos" id="nombreEmpleado"> Carlos <sup>1</sup> 
  <input type="radio" name="nombreEmpleado" value="luis" id="nombreEmpleado"> Luis <sup>2</sup> 
</td>

</form>

<?php 


    require "conexion.php";

    $nombreEmpleado = $_POST["nombreEmpleado"];

    $sqlprueba = "INSERT INTO empleado( nombreEmpleado
                                        )
                                    VALUES(
                                           '".$nombreEmpleado."'
                                        )";

    $resultadoPrueba = mysqli_query($link,$sqlprueba)
                                        or die( mysqli_error($link));
$chequeo = mysqli_affected_rows($link);

mysqli_close($link);

? >


  <input type="radio" name="nombreEmpleado" value=[$nombreEmpleado] checked> <?php echo $nombreEmpleado; ?>

  <input type="radio" name="nombreEmpleado" value="juan" id="nombreEmpleado"> Juan
  <input type="radio" name="nombreEmpleado" value="Carlos" id="nombreEmpleado"> Carlos 
  <input type="radio" name="nombreEmpleado" value="Luis" id="nombreEmpleado" > Luis 
</td>
    
asked by Laliposa 10.02.2018 в 16:19
source

5 answers

1

What you should do is modify your PHP form and use the checked property that have the radio input .

That is:

$juanIsChecked = $nombreEmpleado == 'juan' ? 'checked' : '';
<input type="radio" ... value="juan" $juanIsChecked />

And so with each of the fields.

    
answered by 10.02.2018 в 16:41
1

In the html change this name="nombreEmpleado[]" , that is, add the brackets [] with this you have PHP access the selected values as an array. Then simply, if there is a checkbox nombreEmpleado selected, you put it in a variable $empleado that you go through it in an array to finally paint it in a echo

Greetings

if (isset($_POST['nombreEmpleado'])) {
    $empleados = $_POST['nombreEmpleado'];
    echo 'empleados seleccionados: ';
    for ($i = 0; $i < count($empleados); $i++) {
        echo $empleados[$i] . ' ';
    }
} else {
    echo 'no has marcado ninguna casilla';
}
    
answered by 10.02.2018 в 19:24
0

What I would do in this case, is add one more box to the database table called select with a Boolean value.

Your table would be like this:

Tabla empleados
Id      | Nombre Emp. | Select
1       | Juan        | true

And using a loop generated with the checkboxes or ratio, mark them with checked. And that would be more or less like this:

<?php
require "conexion.php";
$sqlprueba = "SELECT * FROM empleados";
$resultadoPrueba = mysqli_query($link, $sqlprueba) or die( mysqli_error($link));
while ($row = mysqli_affected_rows($link)) {
    $check = $row["select"] === true ? " checked" : "";  
?>
<input type="radio" name="nombreEmpleado[]" value="<?php echo $row["nombreEmpleado"]; ?>"<?php echo $check; ?>> <?php echo $row["nombreEmpleado"]; ?>
<?php
}
mysqli_close($link);
?>

Obviously that would be after having inserted the data, the code that I show is only to show a list of the employees that are already registered in the database.

    
answered by 11.02.2018 в 02:05
0

Thank you very much everyone! I'm going to check every idea you gave me, but yesterday I was able to solve it in this way, I send it to you in case someone falls into the same question as me and needs a solution, maybe it will help. Greetings!

<input type="radio" name="nombreEmpleado" value="juan" <?php echo ($nombreEmpleado=='juan')?> checked>juan

(and so with each input)

    
answered by 11.02.2018 в 17:51
-1

This was the solution I found:

<input type="radio" 
       id="t-option" 
       name="selector"  
       value="Envio Express ( 1 - 2 dias )" 
       <?php 
         if($temporal["envio"]=='Envio Express ( 1 - 2 dias )'){
           echo "checked";
         }
       ?>
       >
    
answered by 17.08.2018 в 19:36