simple php calculator + ajax [duplicate]
تبليغيرجى شرح بإيجاز لمإذا تشعر أنك ينبغي الإبلاغ عن هذا السؤال.
I’m creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it’s not outputting the answer to the browser.
Some one can help me, idk why this code not working.
index.php
<!DOCTYPE html>
<body>
number 1: <input type=”text” id=”num1″ > <br> </br>
nubmer 2: <input type=”text” id=”num2″ > <br> </br>
<button type=”button” onclick=”calc(‘+’)”> + </button>
<button type=”button” onclick=”calc(‘-‘)”> – </button>
<button type=”button” onclick=”calc(‘*’)”> * </button>
<button type=”button” onclick=”calc(‘/’)”> / </button>
<br> </br>
result : <span id=”result”> </span>
<script>
function calc(reqtype) {
var xhr = new XMLHttpRequest();
var num1 = document.getElementById(“num1”).value;
var num2 = document.getElementById(“num2”).value;
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
document.getElementById(“result”).innerHTML = xhr.responseText;
}
xhr.open(“post”, “c.php?”, true);
xhr.setRequestHeader(“content-type”, “application/x-www-form-urlencoded”);
xhr.send(“&req=”+reqtype+”&num1=”+num1+”&num2=”+num2);
}
}
</script>
</body>
</html>
second php file
c.php
<?php
$num1=$_REQUEST[“num1”];
$num2=$_REQUEST[“num2”];
$req=$_REQUEST[“result”];
if ($req==”+”) {
echo $num1+$num2;
}
elseif ($req==”-“) {
echo $num1*$num2;
}
elseif ($req==”*”) {
echo $num1*$num2;
}
elseif ($req==”/”) {
echo $num1/$num2;
}
?>
أضف إجابة