自制计算器不起作用

Self-made calculator doesnt work

本文关键字:不起作用 计算器      更新时间:2023-09-26

我用JS和PHP编写了小计算器脚本。如我所见,一切都是正确的,但在输出服务器中显示空白('0'(值。我在 2 小时内找不到解决方案。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>

计算表格:

<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()" />

用于检查的计算.php:

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

这一行:

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

is_numeric 的返回值分配给$a  — 例如,truefalse 。然后,你使用$a好像它包含发布到脚本的值,但它没有。

您可能打算使用intvalfloatval或类似

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

请注意,除非您需要支持非常旧的浏览器,否则不需要您的getXmlHttp功能。所有模糊的现代浏览器都支持new XMLHttpRequest