<?php
// Se for uma requisição AJAX (sem recarregar a página)
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["valor"])) {
$valor = floatval($_POST["valor"]);
$resultado = $valor * 0.13;
echo number_format($resultado, 2, ',', '.');
exit; // Encerra aqui para não carregar o HTML abaixo
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Calculadora</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f5f5f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: #fff;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
text-align: center;
width: 300px;
}
h2 {
margin-bottom: 20px;
color: #333;
}
input[type="number"] {
padding: 10px;
width: 100%;
border: 1px solid #ccc;
border-radius: 8px;
margin-bottom: 15px;
font-size: 16px;
}
button {
padding: 10px;
width: 100%;
background: #007BFF;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
}
button:hover {
background: #0056b3;
}
.resultado {
margin-top: 20px;
font-size: 18px;
font-weight: bold;
color: #28a745;
}
</style>
</head>
<body>
<div class="container">
<h2>Calculadora 13%</h2>
<input type="number" id="valor" step="0.01" placeholder="Digite um número" required>
<button onclick="calcular()">Calcular</button>
<div class="resultado" id="resultado"></div>
</div>
<script>
function calcular() {
let valor = document.getElementById("valor").value;
if (valor === "") {
document.getElementById("resultado").innerHTML = "Digite um número!";
return;
}
let xhr = new XMLHttpRequest();
xhr.open("POST", "", true); // envia para o próprio arquivo
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById("resultado").innerHTML = "Resultado: " + xhr.responseText;
}
};
xhr.send("valor=" + valor);
}
</script>
</body>
</html>