I am quite a beginner in PHP and have created a calculator and I am posting the calculation as a variable(q) to a php file and want to make the calculation and echo it back to the box as the result.
Going through the php function library I could not locate a function the can help me with that like 'eval' in JavaScript.
The reason I am not using 'eval' is that the calculation must happen on the server side.
Any help will be greatly appreciated.
JAVASCRIPT
$('.equal').click(function(){
$.post('calculator.php',{q: $('.result').val()} ,function(a){
$('.result').val(a).show();
});
});
PHP
<?php
$_POST[q];
?>
HTML
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>calculator</title>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css'>
<link rel='stylesheet prefetch' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css'>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container col-sm-2 text-center" id="calc">
<div class="row">
<form name="calc">
<input type="text" class="screen text-right result" name="result" placeholder="0" readonly>
</form>
</div>
<!-- First row -->
<div class = "row">
<button class="btn btn-primary number" data-key="1" type="button">1</button>
<button class="btn btn-primary number" data-key="2" type="button">2</button>
<button class="btn btn-primary number" data-key="3" type="button">3</button>
<button class="btn btn-danger" data-key="reset" type="button">C</button>
</div>
<!-- Second row -->
<div class = "row">
<button class="btn btn-primary number" data-key="4" type="button">4</button>
<button class="btn btn-primary number" data-key="5" type="button">5</button>
<button class="btn btn-primary number" data-key="6" type="button">6</button>
<button class="btn btn-warning number" data-key="+" type="button">+</button>
</div>
<!-- Third row -->
<div class = "row">
<button class="btn btn-primary number" data-key="7" type="button">7</button>
<button class="btn btn-primary number" data-key="8" type="button">8</button>
<button class="btn btn-primary number" data-key="9" type="button">9</button>
<button class="btn btn-warning number" data-key="-" type="button">-</button>
</div>
<!-- Fourth row-->
<div class = "row">
<button class="btn btn-primary number" data-key="0" type="button">0</button>
<button class="btn btn-primary number" data-key="." type="button">.</button>
<button class="btn btn-warning number" data-key="/" type="button">/</button>
<button class="btn btn-warning number" data-key="*" type="button">*</button>
</div>
<div class="row">
<button class="btn btn-success btn-lg equal" type="button">=</button>
</div>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js'></script>
<script src="js/calculator.js"></script>
<!-- jquery fade in function -->
<script>
$( "#calc" ).hide( 500 ).delay( 1500 ).show( 300 );
</script>
</body>
</html>