1
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title>Calc PHP</title>
</head>
<body>
<div>
<form method="post" action="index.php" id="form">
Valor 1: <input type="number" name="num1" id="v1"><br><br>
Valor 2: <input type="number" name="num2" id="v2"><br><br>
<select name="cal" id="campo">
<option value="add">Somar</option>
<option value="sub">Subtrair</option>
<option value="mult">Multiplicar</option>
<option value="div">Dividir</option>
</select>
<button type="button" value="submit" onclick="mostraResultado()">Calcular</button>
</form>
<script type="text/javascript">
function mostraResultado(){
var form = document.getElementById("form");
var valor1 = document.getElementById("v1");
var valor2 = document.getElementById("v2");
var opcao = document.getElementById("campo");
switch(opcao){
case "add":
var resultado = valor1.value + valor2.value;
document.write("O resultado é " + resultado.value);
break;
case "sub":
var resultado = valor1.value - valor2.value;
document.write("O resultado é " + resultado.value);
break;
case "mult":
var resultado = valor1.value * valor2.value;
document.write("O resultado é " + resultado.value);
break;
case "div":
var resultado = valor1.value / valor2.value;
document.write("O resultado é " + resultado.value);
break;
}
}
</script>
</div>
</body>
</html>
What’s the mistake buddy?
– Maycon F. Castro
Post the html tbm.
– LeAndrade
So, it’s a very simple thing that I want, the form takes both values and chooses which operation it wants to perform, then presses the send button. So far so good, but the problem is that I want to show you the result of the calculation below the button, like "The result gave such". And I can’t do that. It doesn’t show up when I press send
– Aly98
It will not appear because your button is a Ubmit. Change the type of your button to the same button... <button type="button"...
– André Luis Marmo
I fixed it, but nothing comes up anyway
– Aly98
valor1andvalor2are the elements of the GIFT, you do not need to take their values, no? Something likevalor1.value + valor2.value?– Woss
I’ve already changed it, and yet there’s nothing appearing
– Aly98
Then update your question with the current code.
– Woss
I’ve updated the code
– Aly98
Why
resultado.value? Please pay more attention to what you are doing and especially understand what you are doing. Trial and error will lead to nothing.– Woss
The following is an example: https://jsfiddle.net/r6kjsbmv/5/
– André Luis Marmo