Reputation:
I wrote small calculator script with JS and PHP. As i saw all is correct, but in output server show me blank ('0') value. I cant find a solution for 2 hours. JS script, that open connection with POST method with action 'calc.php':
<script type="text/javascript">
function getXmlHttp() {
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
function summa() {
var how0 = document.getElementById("how0").value;
var xmlhttp = getXmlHttp();
xmlhttp.open('POST', 'calc.php', true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("how0=" + encodeURIComponent(how0));
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("how").value = xmlhttp.responseText; //
}
}
};
}
</script>
Form for calculating:
<input type="number" required name="how0" id="how0" min="100" max="999999" placeholder="100">
<input type="number" required readonly name="how" id="how" min="200" max="999999" placeholder="200">
<input type="button" value="Calcul" onclick="summa()" />
Calc.php for checking:
<?php
$a = is_numeric($_POST["how0"]);
$a = floor($a);
if ($a>1) {
$a = $a * 2;
}
if ($a>10000) {
$a = $a * 3;
}
echo $a;
?>
Upvotes: 1
Views: 72
Reputation: 1074268
This line:
$a = is_numeric($_POST["how0"]);
Assigns the return value of is_numeric
to $a
— e.g., true
or false
. Then you use $a
as though it contained the value posted to the script, but it doesn't.
You probably meant to use intval
or floatval
or similar:
$a = intval($_POST["how0"]);
Note that unless you need to support really old browsers, there's no need for your getXmlHttp
function. All vaguely-modern browsers support new XMLHttpRequest
.
Upvotes: 3