该程序正在跳过if if语句中的if

时间:2018-09-24 12:29:14

标签: php html5 if-statement

这是我的php代码:

        <?php
            $amount = $_GET["startingAmount"];
            $whichWay = $_GET["whichWay"];
            $conversion = $_GET["conversion"];
            $step = $_GET["step"];
            if (is_numeric($conversion)) 
            {
                if ($whichWay === "1") 
                {
                    echo "<table>";
                        echo "<tr><th>USD</th><th>Canadian</th></tr>";
                        for ($i = 0; $i <= 10; $i++) 
                        {                        
                            $outputValue = $amount * $conversion;
                            echo "<tr><td>$amount</td><td>$outputValue</td></tr>";
                            $amount = $amount+$step;
                        }
                    echo "</table>";
                } 
                else
                {
                    echo "<table>";
                        echo "<tr><th>Canadian</th><th>USD</th></tr>";
                        for ($i = 0; $i <= 10; $i++) 
                        {                        
                            $outputValue = $amount * $conversion;
                            echo "<tr><td>$amount</td><td>$outputValue</td></tr>";
                            $amount = $amount+$step;
                        }
                    echo "</table>";
                }
            }
            else 
            {
                    echo "<p>You did not input a numeric value for both the amount you wanted to convert and for the conversion rate.<br>Please go back and do it again</p>";
            }
        ?>

以下是发送信息的html表单:

<body>
        <h1>USD/Canadian Money Conversion Site</h1>
        <div>
            <form method="get" action="output.php">            
                Starting Amount: 
                    <select name="startingAmount" size="1">
                        <option name="1">1</option>
                        <option name="10">10</option>
                        <option name="20">20</option>
                    </select><br> 
                Conversion type:
                    <select name="whichWay" size="1">
                        <option name="1">USD to Canadian</option>
                        <option name="2">Canadian to USD</option>
                    </select><br>  
                Conversion Rate: <input type="text" name="conversion" required><br>
                Step:
                    <select name="step" size="1">
                        <option name="1">1</option>
                        <option name="2">2</option>
                        <option name="3">3</option>
                        <option name="4">4</option>
                    </select><br> 
                <input type="submit">
            </form>
        </div>
    </body>

由于某些原因,如果用户选择美元兑换加拿大元,反之亦然,php则无法识别whichWay的值,而仅运行else语句。 其他一切正常。该程序可以运行else语句,其余变量可以正常工作。 任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:3)

<option name="1">更改为<option value="1">,并对所有<option元素执行相同的操作。

答案 1 :(得分:0)

您在HTML代码中具有“名称”,但没有“值”。

在“选择”中的每个选项中,而不是“ name ='1'”,而应使用“ value ='1'”,等等

答案 2 :(得分:0)

HTML中的

Option elements不使用name属性,而是使用value属性。

您的HTML应该更改以反映这一点;

<form method="get" action="output.php">            
    Starting Amount: 
        <select name="startingAmount" size="1">
            <option value="1">1</option>
            <option value="10">10</option>
            <option value="20">20</option>
        </select><br> 
    Conversion type:
        <select name="whichWay" size="1">
            <option value="1">USD to Canadian</option>
            <option value="2">Canadian to USD</option>
        </select><br>  
    Conversion Rate: <input type="text" name="conversion" required><br>
    Step:
        <select name="step" size="1">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
        </select><br> 
    <input type="submit">
</form>

您的PHP代码也可以得到很大的改善,这与您当前的代码等效,但是您可以看到,它更短,并且减少了重复代码。只需将新类型添加到$types数组中,即可更轻松地扩展它。

<?php
    $amount = $_GET["startingAmount"];
    $whichWay = $_GET["whichWay"];
    $conversion = $_GET["conversion"];
    $step = $_GET["step"];

    $types = [
        ["USD", "Canadian"],
        ["Canadian", "USD"]
    ];

    if (is_numeric($conversion)) 
    {
        echo "<table>";

            $type = $types[$whichWay - 1];
            //$type will now == ["USD", "Canadian"] (if $whichWay is 1), so you can use array indexes to select the proper label

            echo "<tr><th>{$type[0]}</th><th>{$type[1]}</th></tr>";
            for ($i = 0; $i <= 10; $i++) 
            {                        
                $outputValue = $amount * $conversion;
                echo "<tr><td>$amount</td><td>$outputValue</td></tr>";
                $amount = $amount+$step;
            }
        echo "</table>";
    } else {
        echo "<p>You did not input a numeric value for both the amount you wanted to convert and for the conversion rate.<br>Please go back and do it again</p>";
    }
?>