Skip to content
Advertisement

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…

JavaScript

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:xampphtdocsdashboardproyectosPHPindex.php on line 53

Warning: Undefined variable $posicion in C:xampphtdocsdashboardproyectosPHPindex.php on line 54

Warning: Undefined variable $puntaje in C:xampphtdocsdashboardproyectosPHPindex.php on line 55 foto

Advertisement

Answer

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

JavaScript

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.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement