Skip to content

Instantly share code, notes, and snippets.

@johnifegwu
Last active April 23, 2020 19:23
Show Gist options
  • Save johnifegwu/56c48f578e992da03e63733974204022 to your computer and use it in GitHub Desktop.
Save johnifegwu/56c48f578e992da03e63733974204022 to your computer and use it in GitHub Desktop.
PHP, HTML 5, CSS 3 and JavaScript example (Calculator.PHP) Persists data for 7 days utilizing coockie.
<!DOCTYPE html>
<html>
<head>
<style>
.Head
{
font-size:67px;
width:100%;
background:Gray;
color:Silver
}
.TextBox
{
width:100%;
font-size:67px;
color:Blue
}
.TextBox2
{
width:100%;
font-size:67px;
color:Red
}
.Btn
{
width:24%;
font-size:67px;
color:Green
}
.Btn2
{
width:100%;
font-size:67px;
color:Black
}
</style>
<script>
var op = "+";
function box(query){
return document.getElementById(query)
}
function addNum(){
var num1 = parseFloat(box("xText").value);
var num2 = parseFloat(box("yText").value);
var rslt = box("Result");
var value = 0.0
if ((num1 != 0.0) && (num2 != 0.0))
{
if (op == "+")
{
value = num1+num2;
}else if(op == "-")
{
value = num1-num2;
}else if(op == "*")
{
value = num1*num2;
}else if(op == "/")
{
value = num1/num2;
}
var num = value.toFixed(2).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
rslt.value = num;
op = "+";
box("save").disabled = false;
box("xText").value = "";
box("yText").value = "";
box("xText").focus();
}
}
function setFocus(){
if (screen.height>640)
{
s = (screen.height / 640) * 67;
setResolution(s);
}
var txt = box("xText");
txt.focus();
}
function setResolution(s) {
box("xText").style.fontSize = s + "px";
box("yText").style.fontSize = s + "px";
box("head").style.fontSize = s + "px";
box("btnEnt").style.fontSize = s + "px";
box("btnAdd").style.fontSize = s + "px";
box("btnSub").style.fontSize = s + "px";
box("btnDiv").style.fontSize = s + "px";
box("btnMul").style.fontSize = s + "px";
box("Result").style.fontSize = s + "px";
box("save").style.fontSize = s + "px";
box("total").style.fontSize = s + "px";
}
function doEvent(id){
var b = box(id);
b.onclick();
}
</script>
</head>
<body onload="setFocus()">
<?php
// define variables
$emptyStr = "";
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == "POST")
{
if (empty($_POST['Result']) != true)
{
$Result = $_POST['Result'];
// we will save the total for a week
setcookie('Myuser10', $Result, time() + (86400 * 7), '/');
}
}
/*
function test_input($data) {
  $data = trim($data);
  $data = stripslashes($d);
  $data = htmlspecialchars($d);
  return $d;
} */
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">  
<h2 class="Head" ID="head">Calculator</h2>
<input type="Number" class="TextBox" ID="xText" name="xText" placeholder="0.0" onchange="box('yText').focus();" value="<?php echo $emptyStr; ?>"> <br/>
<input type="Number" class="TextBox" ID="yText" name="yText" placeholder="0.0" onchange="box('btnEnt').focus();" value="<?php echo $emptyStr; ?>" ><br/>
<input type="Button" class="Btn" ID="btnAdd" Value="+" onclick="op = box('btnAdd').value;">
<input type="Button" class="Btn" ID="btnSub" Value="-" onclick="op = box('btnSub').value;">
<input type="Button" class="Btn" ID="btnMul" Value="*" onclick="op = box('btnMul').value;">
<input type="Button" class="Btn" ID="btnDiv" Value="/" onclick="op = box('btnDiv').value;">
<br/>
<input type="Button" class="Btn2" ID="btnEnt" Value="Enter" onclick="addNum()">
</br>
<input type="Text" class="TextBox2" ID="Result" name="Result" placeholder="0.0" value="<?php echo $emptyStr; ?>" ></br>
<input disabled="true" type="submit" id="save" name="save" value="Save" class="Btn2" ></br>
<input disabled="true" type="text" id="total" name="total" class="TextBox2" placeholder="0.0" value="<?php if(isset($_COOKIE['Myuser10'])) {echo $_COOKIE['Myuser10'];} else {echo '0.0';} ?>" >
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment