Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
O erro de subtração ja corrigi! :)
Galera tenho um erro no local em que ele mostra o resultado, pois quero que fique a mostra no campo direito da opção valor2, mais ele fica em baixo do botão calcular ele não entra na opção <input type="text" name="total" id="total" size="5" maxlength="10" />.
Como posso consertar esse meu erro!
Galera sou amador e apos ter conseguido fazer este funcionar perfeitamente, vou tentar criar uma calculadora em .php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Calculadora</title>
</head>
<body>
<form>
<table>
<tr>
<td><h4>Calculadora</h4></td>
</tr>
<tr>
<td colspan="2"><hr /></td>
</tr>
<tr>
<td>Valor1
<input type="text" name="valor1" id="valor1" size="5" maxlength="5" />
<td><select name="opcao" id="opcao">
<option>Selecione</option>
<option value="somar">+ somar</option>
<option value="subtrair">- subtrair</option>
<option value="multiplicar">* multiplicar</option>
<option value="dividir">/ dividir</option>
</select>
Valor2
<input type="text" name="valor2" id="valor2" size="5" maxlength="5" />
=
<input type="text" name="total" id="total" size="5" maxlength="10" /></td>
</tr>
<tr>
<td><input type="submit" name="igual" id="igual" value="calcular" /> </td>
</tr>
</table>
</form>
<?php
$opcao = $_GET['opcao'];
$total = $_GET['total'];
if ($opcao == somar){
$total = ($valor1 + $valor2);
echo "$total";
}
elseif ($opcao == subtrair){
$total = ($valor1 - $valor2);
echo "$total";
}
elseif ($opcao == multiplicar){
$total = ($valor1 * $valor2);
echo "$total";
}
elseif ($opcao == dividir){
$total = ($valor1 / $valor2);
echo "$total";
}
else {
echo "Opção invalida";
}
?>
</body>
</html>Carregando comentários...