Really when you find an error in break
, that's not the problem, but the previous line (and in almost all cases, it's usually that way).
They have already commented to you several of the errors:
- Missing quotation marks (single or double) to indicate the presence of a String.
- Two points missing at the end of the assignment to the variable (What causes the error with
break
).
I would add, for aesthetic and efficiency reasons.
- Declare variable
user
only once, outside the switch with a dummy value or no value. Surely you work with her outside the switch and you do not want to have problems if it is undefined
.
- The
break
after default
. It is not necessary at all, but it is not a bad habit.
And to continue with debugging errors, I remind you of two things:
- Javascript errors are only visible in the browser developer console you are using, or the IDE / Platform you use to program.
- PHP errors are varied and depend on the configuration
of the server, but in general they are all printed on the page.
Here is a quick example of how your function would work. Remember that when you print PHP code on the page, it does not appear, but rather executes it. However, the javascript code is usually visible to the user and I do not advise declaring too much PHP code there.
In the example I replace the first <
of the PHP statement with /
so you can visualize it, but remember that this prevents its execution.
function cambiar(){
var user = '';
var id = document.getElementById('v1').value;
switch (id) {
case "0":
user = "<?php echo $vv0;?>";
break;
case "1":
user = "<?php echo $vv1;?>";
break;
case "2":
user = "<?php echo $vv2;?>";
break;
case "3":
user = "<?php echo $vv3;?>";
break;
case "4":
user = "<?php echo $vv4;?>";
break;
default:
user = "<?php echo $vv5;?>";
break;
}
// Borrar esto para que se imprima el tag PHP y se ejecute.
user = user.replace('<','/');
document.getElementById('frase').innerHTML = user;
}
<p id='frase'> Aquí se pone el string de JS </p>
<input text="button" value="0" id="v1"/>
<input type="button" value="Botón" id="b1" onclick="cambiar();"/>