if语句错误显示不同的值

时间:2015-12-04 07:01:49

标签: php html forms

我是PHP的新手,我正在尝试通过制作计算器来研究"if statement",我最终得到了一个错误。结果显示不正确,请帮助建议如何在每次提交后清除输入。

<html !doctype>
    <head>
        <title>

        </title>
    </head>
    <body>
        <?php 
        error_reporting();
        if(isset($_POST['submit'])) {
            $num1 = $_POST['number1'];
            $num2 = $_POST['number1'];
            $action = $_POST['action1'];

            if($action == "addition"){
                $add = $num1 + $num2;
                echo "your value is";
                echo $add;
            }
            if($action == "subtraction"){
                $sub = $num1 - $num2;
                echo "your value is";
                echo $sub;
            }
            if($action =="multipilcation"){
                $multi = $num1 * $num2;
                echo "your value is";
                echo $multi;
            }

            if($action =="division"){
                $divi = $num1 / $num2;
                echo "your value is";
                echo $divi;
            }
        }
        ?>
        <form method='post' name='myform'>
            Enter Number 1:<br>
            <input type="number" name="number1" >
            <br>
            Enter Number 2:<br>
            <input type="number" name="number2" >
            <br>
            choose operation<br>
            <select name='action1'>
                <option>addition</option>>
                <option>subtraction</option>
                <option>multipilcation</option>
                <option>division</option>
            </select>
            <input type="submit" name="submit" value="submit">
        </form>
    </body>
    <footer>

    </footer>
</html>

3 个答案:

答案 0 :(得分:1)

您对两个变量使用了相同的number1,将其更改为number2

$num1 = $_POST['number1'];
$num2 = $_POST['number2'];
                      ^
$action = $_POST['action1'];

答案 1 :(得分:1)

1)您没有在<option></option>标记中传递任何值。

<select name='action1'>
    <option value="addition">addition</option>>
    <option value="subtraction">subtraction</option>
    <option value="multipilcation">multipilcation</option>
    <option value="division">division</option>
</select>

2)将$num2 = $_POST['number1'];更改为$num2 = $_POST['number2'];

<?
if(isset($_POST['submit'])) {

    $num1 = $_POST['number1'];
    $num2 = $_POST['number2'];
    $action = $_POST['action1'];

答案 2 :(得分:0)

$num2 = $_POST['number1'];

chnage with:

 $num2 = $_POST['number2'];

还在select中添加选项标记的value属性。

<option value="addition">addition</option>>
<option value="subtraction">subtraction</option>
<option value="multipilcation">multipilcation</option>
<option value="division">division</option>