0

您好,我正在尝试制作计算器。我遇到了这个问题:我正在尝试做出选择,以便在提交后保留。我在谷歌上找到了一些代码供我选择在提交后保留,但我的计算器不再工作了。你能帮助我吗 ?

这是我的计算器工作时的代码,但我的选择在提交后不会保留

<html>
 <body>
<center>
<form method="post">
    Food:
        <Select name="Dropdown"><br>
        <option>Cheese</option>
        <option>Apple</option>
        <option>Egg</option>
        </select>
       </br> 
    Amount:
        <input name="amount" type="text">grams<br><br>
        <br><input type="Submit" value="Calculate">  
        <br><br> 


        <?php
            $result=$_POST['result'];
                    $Dropdown=$_POST['Dropdown'];
                    $amount = $_POST['amount'];

                switch ($Dropdown){
                        case 'Cheese':
                        $result= (7.74  * $amount) / 100;
                        echo "<p> $result </p>";

                        break;
                        case 'Apple':
                        $result= (1.94  * $amount) / 100;
                        echo "<p> $result </p>";

                        break;
                         case 'Egg':
                        $result= (13.74  * $amount) / 100;
                        echo "<p> $result </p>";

                        }
                        ?> 
        </form> 
        </center> 
          </body>
</html>  

这是我的代码,当我的选择在提交后仍然存在但我的计算器不起作用

<html>
<head></head>
<body>
<center>
<?php
if (!empty($_POST['Dropdown'])) {
    $dropDownVal = $_POST['Dropdown'];
} else {
    $dropDownVal = 1;
}
?>
<form method="post">
    <select name="Dropdown" >
        <option value="1" <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Cheese </option>
        <option value="2" <?php if ($dropDownVal==2) echo 'selected="selected"'; ?>>Apple</option>
        <option value="3" <?php if ($dropDownVal==3) echo 'selected="selected"'; ?>>Egg</option>
   </select>

   <?php
            $result=$_POST['result'];
            $Dropdown=$_POST['Dropdown'];
            $amount = $_POST['amount'];

                switch ($Dropdown){
                        case 'Cheese':
                        $result= (7.74  * $amount) / 100;
                        echo "<p> $result </p>";

                        break;
                        case 'Apple':
                        $result= (1.94  * $amount) / 100;
                        echo "<p> $result </p>";

                        break;
                         case 'Egg':
                        $result= (13.74  * $amount) / 100;
                        echo "<p> $result </p>";

                        }
                        ?> 



    <input name="amount" type="text">grams<br><br>
        <br><input type="Submit" value="Calculate">
</form>
</center>
</body>
</html>

谢谢

4

1 回答 1

3

您在 $_POST['Dropdown'] 中收到的是值而不是内容,因此您将获得 1 或 2 或 3 而不是奶酪、苹果或鸡蛋。

所以试试:

    switch ($Dropdown){
    case 1: // Cheese
         $result= (7.74  * $amount) / 100;
         break;
    case 2:  //Apple
         $result= (1.94  * $amount) / 100;
         break;
    case 3:  // Egg
         $result= (13.74  * $amount) / 100;
         break;
    default:
         $result = 0;
    }
    echo "<p> $result </p>";
?> 

将来,如果您不确定从用户返回的变量中的内容,请执行

echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';

在浏览器上很好地显示数组。或者一个

var_dump( $var );
于 2013-11-12T10:31:25.267 回答