简体   繁体   English

自制的计算器不起作用

[英]Self-made calculator doesnt work

I wrote small calculator script with JS and PHP. 我用JS和PHP编写了小型计算器脚本。 As i saw all is correct, but in output server show me blank ('0') value. 正如我所看到的一切都是正确的,但在输出服务器中显示空白('0')值。 I cant find a solution for 2 hours. 我找不到2个小时的解决方案。 JS script, that open connection with POST method with action 'calc.php': JS脚本,使用动作'calc.php'打开与POST方法的连接:

  <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: Calc.php用于检查:

<?php
  $a = is_numeric($_POST["how0"]);
  $a = floor($a);
if ($a>1) {
    $a = $a * 2;
}
if ($a>10000) {
    $a = $a * 3;
}
echo $a;

?>

This line: 这一行:

$a = is_numeric($_POST["how0"]);

Assigns the return value of is_numeric to $a — eg, true or false . is_numeric的返回值分配给$a - 例如, truefalse Then you use $a as though it contained the value posted to the script, but it doesn't. 然后你使用$a ,好像它包含发布到脚本的值,但它没有。

You probably meant to use intval or floatval or similar: 您可能打算使用intvalfloatval或类似的:

$a = intval($_POST["how0"]);

Note that unless you need to support really old browsers, there's no need for your getXmlHttp function. 请注意,除非您需要支持真正的旧浏览器,否则不需要使用getXmlHttp函数。 All vaguely-modern browsers support new XMLHttpRequest . 所有模糊的现代浏览器都支持new XMLHttpRequest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM