Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
php- switch fails before i select an option

I'm having a problem with php switch function. I know it must be simple, but i'm just learning and I can't seem to find the right way to code it. This is the issue: It works fine when you click any of the options, but it sends an error msg when I first load up the page...

<body>
    <?php include ('./header.php')?>

    <section>
        <div class="btn">
            <ul>
                <li><a href="./index.php?opcion=op1">Opcion 1</a></li>
                <li><a href="./index.php?opcion=op2">Opcion 2</a></li>
                <li><a href="./index.php?opcion=op3">Opcion 3</a></li>
            </ul>
        </div>

        <?php 
            if(isset ($_GET['opcion'])) {

            
            switch($_GET['opcion']) {
                case 'op1' :
                    $nombre = 'Magnus Carlsen';
                    $posicion = '1';
                    $puntaje = '2840';
                    $img = './magnus.jpg';
                break;
                case 'op2' :
                    $nombre = 'Ian Nepomniatchi';
                    $posicion = '2';
                    $puntaje = '2812';
                    $img = './nepo.jpeg';
                break;
                case 'op3' :
                    $nombre = 'Ding Liren';
                    $posicion = '3';
                    $puntaje = '2760';
                    $img = './ding.jpg';
                break;
                }
            }
            else {
                
            }
            
            ?>
             <div class="show">
                    <h3><?php echo $nombre?></h2>
                    <h4><?php echo $posicion?></h4>
                    <p><?php echo $puntaje?></p>
                    <div class="contenedor_img">
                        <img src="<?php echo $img ?>" alt="foto">
                    </div>
                </div>

Now, I suppose the "show" div shouldn't be there... but it doesn't responde well when i move it around either...

this are the errors:

Warning: Undefined variable $nombre in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 53

Warning: Undefined variable $posicion in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 54

Warning: Undefined variable $puntaje in C:\xampp\htdocs\dashboard\proyectos\PHP\index.php on line 55 foto

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The problem is that when the form is not submitted, you do not enter your switch. that means your $nombre, $posicion, $puntaje and most probably $img are not defined. There are 2 ways to unblock yourself.

Option 1, give a default value to these variables

// here you can define your variables
$nombre = 'no number';
$posicion = 'no position';
$puntaje = 'empty';
$img = '';
if (isset ($_GET['opcion'])) {
    switch ($_GET['opcion']) {
        case 'op1' :
            $nombre = 'Magnus Carlsen';
            $posicion = '1';
            $puntaje = '2840';
            $img = './magnus.jpg';
            break;
        case 'op2' :
            $nombre = 'Ian Nepomniatchi';
            $posicion = '2';
            $puntaje = '2812';
            $img = './nepo.jpeg';
            break;
        case 'op3' :
            $nombre = 'Ding Liren';
            $posicion = '3';
            $puntaje = '2760';
            $img = './ding.jpg';
            break;
    }
} else {
}

or you can check to see if user has submitted the form and only then try to echo the variables. You can do this by surrounding the html in an if statement.

<?php if(isset ($_GET['opcion'])) { ?>
<div class="show">
    <h3><?php echo $nombre?></h2>
        <h4><?php echo $posicion?></h4>
        <p><?php echo $puntaje?></p>
        <div class="contenedor_img">
            <img src="<?php echo $img ?>" alt="foto">
        </div>
</div>
<?php } ?>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda