link depending on the radio button selected in PHP and HTML

0

hello everyone would like to know how to make a link depending on the selected radio button

<section id="form">
    <form action="votarEncuesta.php" class="contact_form" name="form1" method="post">
        <table>
            <tr>
                <td width="50"><input  type="radio" id="Choice1" name="seccion" value="Encuestas"></br></td>  
                <td width="470">Encuestas</td>
            </tr> 
            <tr>
                <td width="50"><input type="radio" id="Choice2" name="seccion" value="Preguntas"></br></td>  
                <td width="470">Preguntas</td>
            </tr> 
            <tr>
                <td>
                    <input type="submit" class="submit" value="Ver" />
                </td>
            </tr>
        </table>
    </form>
</section>
    
asked by antonio sanchez 05.04.2018 в 21:42
source

2 answers

2

If you want to redirect to a specific page according to your radio selected, you could do it in the following way by PHP :

Form

<form action="votarEncuesta.php" class="contact_form" name="form1" method="post">
    <table>
        <tr>
            <td width="50"><input  type="radio" id="Choice1" name="seccion" value="Encuestas"></br></td>  
            <td width="470">Encuestas</td>
        </tr> 
        <tr>
            <td width="50"><input type="radio" id="Choice2" name="seccion" value="Preguntas"></br></td>  
            <td width="470">Preguntas</td>
        </tr> 
         <tr>
            <td>
                <input type="submit" class="submit" value="Ver" />
            </td>
        </tr>
    </table>
</form>

voteEncuesta.php

<?php 
//Reseteo.
$seccion = '';
//Si está definido el formulario.
if (isset($_POST)) {
    //Comprobamos que no este vacío input.
    if (empty($_POST['seccion'])) {
        echo "Elige una opción. <a href='formulario.php'>Regresar</a>";
    } else {

        //Obtenemos valor input radio.
        $seccion = $_POST['seccion'] ?: '';

        //Redirigimos según opción seleccionado.
        if ($seccion == 'Encuestas') {
            echo '<script>window.location="encuesta.php"</script>';
        } else {
            echo '<script>window.location="preguntas.php"</script>';
        }
    }   
}
?>
    
answered by 05.04.2018 / 22:38
source
0

I add my comment as an answer in case you do not want to use php for redirection.

if(document.getElementById("seccion").value == "Encuestas") {
    window.location("encuestas.php");
} else {
    window.location("preguntas.php");
}
    
answered by 06.04.2018 в 14:55