display the selected value of a checkbox on the screen

0

I want to show on screen a selected value of a checkbox once a form has been sent.

HTML: 



<html>
    <body bgcolor="silver">
        <meta charset="utf-8">

    <h2>Formulario:</h2>
    <h2>
    <form action="formpost.php" method="POST">
        Nombre:<br>
        <input type="text" name="nombre"><br><br>
        E-mail:<br>
        <input type="email" name="email"><br><br>
        Contraseña:<br>
        <input type="password" name="password"><br><br>
        Edad:<br>
        <input type="text" name="Edad"><br><br>
        Educacion:<br>
        <select name="educacion">
            <option value="sin-estudios">Sin estudios</option>
            <option value="Educación-Basica">Educación Basica</option>
            <option value="educacion-Media">Educación Media</option>
            <option value="formacion-profesional">Formación profesional</option>
            <option value="universidad">Universidad</option>
        </select> <br><br>
        Nacionalidad:<br>
        <input type="radio" name="nacionalidad" value="Chilena">Chilena</input>
        <input type="radio" name="nacionalidad" value="otra">Otra</input>
    <input type="text" name="nacionalidad">
        <br><br>

        Idiomas:<br>
        <input type="checkbox" name="idiomas[]" value="español">Español</input>
        <input type="checkbox" name="idiomas[]" value="inglés">Inglés</input>
        <input type="checkbox" name="idiomas[]" value="francés">Francés</input>
        <input type="checkbox" name="idiomas[]" value="aleman">Alemán</input><br><br>

        <input type="submit" name="submit" value="Enviar">
    </form>
    </center>
    </body>
    </html>

PHP code

 <?php echo $_POST["nombre"]; ?><br>

    Tu E-mail es: 
        <?php echo $_POST["email"]; ?><br>
        Tu Contraseña es: 
         <?php echo $_POST["password"]; ?><br>

     Edad: 

     <?php echo $_POST["Edad"]; ?><br>

    Estado Academico:

    <?php echo $_POST["educacion"]; ?><br>

     <?php echo $_POST["nacionalidad"]; ?><br>
    
asked by Esteban 22.12.2018 в 15:32
source

2 answers

0

You could go through the array of the checkboxes to find out which of them was selected and then display it on the screen. It would be like this:

Questions if there is a checkbox marked with the function of php isset() and then you go through it with a foreach

echo "Idiomas: <br>";

if(isset($_POST["idiomas"])){
    foreach($_POST["idiomas"] as $idiomas){
        echo $idiomas;
    }
}
    
answered by 22.12.2018 в 17:59
0

To be able to see the values of a checkbox in multiple ways, you need to create an array and traverse it with a forearch . in the cases of the forearch we have to verify if the checkbox is set with the function isset and we also verify if the language is an array with the function is_array()

Here is a small sample as requested:

if(isset($_POST['idiomas']) && is_array($_POST['idiomas'])){
foreach($_POST['idiomas'] as $idiomas) {
echo $idiomas."<br>";  // hacemos un echo a los valores debueltos en el array de idiomas por checkbox
}
}

It is always advisable to do a var_dump() when a POST call is made, that way you check the sent values.

var_dump function

Forearch function

I hope you find it useful.

    
answered by 22.12.2018 в 18:05