This file is named as index.php, this is suppose to get the values of two textboxes and do the calculation. but after clicking on the submit, the code seems to run on infinite loop and crash.
<!DOCTYPE html>
<html>
<head>
<title>Form Calculator</title>
<script type="text/javascript" src="jquery.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function SubmitFormData(){
var first = $("#first").val();
var second = $("#second").val();
$.post("index.php",{first:first,second:second,},
function(data){
$('#results').html(data);
$('#formcal')[0].reset();
});
}
</script>
</head>
<body>
<ul>
<li><a href="index.php">Calculator</a></li>
<li><a href="bmi.php">Body Mass Index Calculator</a></li>
</ul>
<h1>Simple Calculator</h1>
<form action="" id="formcal" method="post">
<input type="number" id="first" name="first" placeholder="number"/>
<select name="operator" id="operator">
<option value="add">+</option>
<option value = "subtract">-</option>
<option value = "multiply">*</option>
<option value = "division">/</option>
</select>
<input type="number" id="second" name="second" placeholder="number 2"/>
<input type="button" id="submitFormData" onclick="SubmitFormData();" value="Calculate"/>
</form>
<br>
<?php
echo $_POST['first'];
echo $_POST['second'];
if(!empty($_POST['first']) && !empty($_POST['second'])){
$number = $_POST['first'];
$number2 = $_POST['second'];
$operator = (isset($_GET['operator']) ? $_GET['operator'] : null);
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
echo "Answer: ";
//if($_POST['operator'] == 'add'){
if($operator == 'add'){
$complete = $number + $number2;
echo "$number + $number2 = $complete";
}
//if($_POST['operator'] == 'subtract'){
if($operator == 'subtract'){
$complete = $number - $number2;
echo "$number - $number2 = $complete";
}
//if($_POST['operator'] == 'multiply'){
if($operator == 'multiply'){
$complete = $number * $number2;
echo "$number X $number2 = $complete";
}
//if($_POST['operator'] == 'division'){
if($operator == 'division'){
$complete = $number / $number2;
echo "$number / $number2 = $complete";
}
}
?>
<div id="results">
</div>
</body>
</html>
This file is named as index.php, this is suppose to get the values of two textboxes and do the calculation. but after clicking on the submit, the code seems to run on infinite loop and crash.