Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Created June 27, 2022 12:14
Show Gist options
  • Save DragonOsman/7ecdc6d969d1d515622f7de30258a75a to your computer and use it in GitHub Desktop.
Save DragonOsman/7ecdc6d969d1d515622f7de30258a75a to your computer and use it in GitHub Desktop.
<?php
if (isset($_POST["num1"]) && isset($_POST["num2"]) && isset($_POST["op"]))
{
print_r($_POST);
$first_operand = $_POST["num1"];
$second_operand = $_POST["num2"];
$operator = $_POST["op"];
header("Content-Type: application/x-www-form-urlencoded");
$result = 0;
if ($operator == "+")
{
$result = $first_operand + $second_operand;
}
else if ($operator == "-")
{
$result = $first_operand - $second_operand;
}
else if ($operator == "*")
{
$result = $first_operand * $second_operand;
}
else if ($operator == "/")
{
$result = $first_operand / $second_operand;
}
echo json_encode($result);
}
else
{
echo("Post data not received.");
}
?>
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>DragonOsman Calculator</title>
<link href="styles/styles.css" rel="stylesheet" />
<script src="scripts/scripts.js" async></script>
</head>
<body>
<main>
<form action="calculator.php" method="post">
<label for="num1">First Operand:</label>
<input name="num1" type="number" id="num1" class="num-in1" />
<label for="num2">Second Operand:</label>
<input name="num2" type="number" id="num2" class="num-in2" />
<label for="op">Operator (enter operator symbol (+, -, *, /)):</label>
<input type="text" name="op" class="op" id="op" />
<label for="result">Result:</label>
<input name="result" type="text" id="result" class="result" disabled />
<input type="submit" value="Calculate" class="submit" />
</form>
</main>
</body>
</html>
"use strict";
fetch("http://localhost/php1/calculator.php", {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json"
}
})
.then(response => response.json())
.then(data => {
console.log(data);
const resultField = document.getElementById("result");
resultField.value = data;
})
.catch(err => console.log(err.message));
* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
}
input:focus {
outline: none;
}
body {
line-height: 1.5;
font-family: Helvetica, "Helvetica Neue", Arial, sans-serif;
font-size: 14px;
background-color: #161619;
color: #fff;
}
form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-top: 5vh;
}
.num-in1, .num-in2, .result, .op {
width: 300px;
height: 30px;
border-radius: 2.5px;
padding-left: 3px;
padding-right: 3px;
}
.btn-container {
display: flex;
justify-content: center;
align-items: center;
}
.submit {
padding: 20px;
background-color: #0f52ba;
color: #fff;
margin-top: 15px;
margin-right: 2px;
width: 100px;
border-radius: 2.5px;
}
.submit:hover {
background-color: #2e5a88;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment