简体   繁体   English

PHP简单计算器

[英]Php simple calculator

i am a beginner in php ,i am trying to do a calculator but something is wrong .i don´t know what is it .when i click the butoon submit the fields turn empty . 我是php的初学者,我正在尝试做一个计算器,但是出了点问题。我不知道这是什么。当我单击按钮时,将字段变为空。

<head>
<title>Funcion isset()</title>
</head>
<body>

    <form name="forma" method="post">
        Realitza una operacio: <br> <br/>
        <input type="text" name="num1"> </t> Operador 1 <br /></br/>
        <input type="text" name="num2"> </t> Operador 2 <br /></br>
        Operació:</br></br>
            <input type="radio" name="opcion" value="+">Suma  <br /></br/>
            <input type="radio" name="opcion" value="-">Resta <br /></br/>
            <input type="radio" name="opcion" value="*">Multiplicacio <br /></br/>
            <input type="radio" name="opcion" value="/">Divisio <br /></br/>
            <input type="radio" name="opcion" value="p">Potencia <br /></br/>
            <input type="submit" name "submit" value="Calcular">
    </form>
<?php
        if (isset($_POST['calcular']))
              { 
                $n1 = $_POST['numero1'];
                $n2 = $_POST['numero2'];
                $oper = $_POST['opcion'];
                 switch ($oper) {
                    case "+":echo  $n1 + $n2;break;
                    case "-": echo $n1 - $n2; break;
                    case "*": echo $n1 * $n2;break;
                    case "/":echo $n1 / $n2;break;
                    case "p": echo pow( $n1,$n2);break;
               }
              }     
?>
</body>
</html>

any help would be appreciated . 任何帮助,将不胜感激 。

There are two obvious problems here: 这里有两个明显的问题:

  1. Instead of <input type="submit" name "submit" value="Calcular"> you have to fix the syntax: name="submit" 代替<input type="submit" name "submit" value="Calcular">您必须修复语法: name="submit"

  2. And you have to check for a post variable with name "submit": if (isset($_POST['submit'])) 并且您必须检查名称为“ submit”的发布变量: if (isset($_POST['submit']))

In general, to debug such problem is is always the first check to really look what values you are actually working with. 通常,调试此类问题始终是真正了解您实际使用的值的第一个检查。 In this case this means: what is actually continaed in $_POST ? 在这种情况下,这意味着: $_POST实际上是什么? So the first step would be to dump that array using var_dump($_POST); 因此,第一步将是使用var_dump($_POST);转储该数组var_dump($_POST); or similar. 或类似。

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

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