下拉菜单很奇怪

时间:2016-07-04 16:28:05

标签: php html5

任何PHP专家:我是初学者。我有这个参考电极转换器:

<!DOCTYPE html>
<html>
    <head>
        <title>Referance Electrode Converter</title>
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
        <h1>Referance Electrode Converter</h1>
        <h6>After Pressing Convert The Dropdown Goes Back to It's Defaults</h6>
        <form action="index.php" method="get">
            <input type="text" name="input" placeholder="Enter Amount"></input>
            <select name="dropdown">
                <option value="SHE" selected><sub>SHE</sub></option>
                <option value="SCE"><sub>SCE</sub></option>
                <option value="AG"><sub>Ag/AgCl</sub></option>
                <option value="MSE">MSE</option>
                <option value="CSE">CSE</option>
            </select>
            <br />
            <?php

$cc_input = $_GET['input'];
$cc_dropdown = $_GET['dropdown'];
$cc_odropdown = $_GET['odropdown'];

if(isset($_GET['sbmt']))
{
    if($cc_dropdown == 'SHE') {
        if($cc_odropdown == 'SHE') {
            $output = $cc_input * 1 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'SCE') {
            $output = $cc_input + 0.2415 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'AG') {
            $output = $cc_input + 0.222 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'MSE') {
            $output = $cc_input - 0.615 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'CSE') {
            $output = $cc_input - 0.318 . "V";
            echo "<input type='text' value='$output' disabled/>";
        }
    }
    elseif($cc_dropdown == 'SCE') {
        if($cc_odropdown == 'SHE') {
            $output = $cc_input - 0.2415 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'SCE') {
            $output = $cc_input * 1 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'AG') {
            $output = $cc_input + 0.0195 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'MSE') {
            $output = $cc_input - 0.3735 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'CSE') {
            $output = $cc_input - 0.765 . "V";
            echo "<input type='text' value='$output' disabled/>";
        }

    }
    elseif($cc_dropdown == 'AG') {
        if($cc_odropdown == 'SHE') {
            $output = $cc_input - 0.222 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'SCE') {
            $output = $cc_input - 0.0195;
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'AG') {
            $output = $cc_input * 1 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'MSE') {
            $output = $cc_input - 0.393 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'CSE') {
            $output = $cc_input - 0.096 . "V";
            echo "<input type='text' value='$output' disabled/>";
        }
    }
    elseif($cc_dropdown == 'MSE') {
        if($cc_odropdown == 'SHE') {
            $output = $cc_input + 0.615 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'SCE') {
            $output = $cc_input + 0.3735 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'AG') {
            $output = $cc_input + 0.393 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'MSE') {
            $output = $cc_input * 1 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'CSE') {
            $output = $cc_input + 0.297 . "V";
            echo "<input type='text' value='$output' disabled/>";
        }
    }
    elseif($cc_dropdown == 'CSE') {
        if($cc_odropdown == 'SHE') {
            $output = $cc_input + 0.318 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'SCE') {
            $output = $cc_input + 0.0765 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'AG') {
            $output = $cc_input + 0.096 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'MSE') {
            $output = $cc_input - 0.297 . "V";
            echo "<input type='text' value='$output' disabled/>";
        } elseif($cc_odropdown == 'CSE') {
            $output = $cc_input * 1 . "V";
            echo "<input type='text' value='$output' disabled/>";
        }
    }
}
            ?>
            <select name="odropdown">
                <option value="SHE"><sub>SHE</sub></option>
                <option value="SCE" selected><sub>SCE</sub></option>
                <option value="AG"><sub>Ag/AgCl</sub></option>
                <option value="MSE">MSE</option>
                <option value="CSE">CSE</option>
            </select>
            <br />
            <input type="submit" name="sbmt" value="Convert!"></input>
        </form>
    </body>
</html>

奇怪的是,当我点击&#34;转换,&#34;下拉菜单返回HTML&#34;选择&#34;变量是。我该怎么做才能解决这个问题?

由于 -Lorelo

1 个答案:

答案 0 :(得分:2)

好的,我不确定这是否只是我服务器上的一个问题,但是当我尝试获取未定义的GET请求变量时它会出错,所以我这样做了:

if(isset($_GET['input'])) $cc_input = $_GET['input'];
if(isset($_GET['dropdown'])) $cc_dropdown = $_GET['dropdown'];
if(isset($_GET['odropdown'])) $cc_odropdown = $_GET['odropdown'];

无论如何,为了解决您的原始问题,您应该检查$ cc_dropdown和$ cc_odropdown的值是什么。根据这一点,您应该决定回显“selected”属性的位置。

<强>更新

这就是我的所作所为:

...
    <body>
    <?php

if(isset($_GET['input'])) $cc_input = $_GET['input'];
if(isset($_GET['dropdown'])) $cc_dropdown = $_GET['dropdown'];
if(isset($_GET['odropdown'])) $cc_odropdown = $_GET['odropdown'];

    ?>
        <h1>Referance Electrode Converter</h1>
        <h6>After Pressing Convert The Dropdown Goes Back to It's Defaults</h6>
        <form action="index.php" method="get">
            <input type="text" name="input" placeholder="Enter Amount"></input>
            <select name="dropdown">
            <?php if(!isset($_GET['sbmt'])): ?>
                <option value="SHE" selected><sub>SHE</sub></option>
                <option value="SCE"><sub>SCE</sub></option>
                <option value="AG"><sub>Ag/AgCl</sub></option>
                <option value="MSE">MSE</option>
                <option value="CSE">CSE</option>
            <?php else: ?>
                <option value="SHE" <?php if($cc_dropdown=="SHE") echo "selected";  ?> ><sub>SHE</sub></option>
                <option value="SCE" <?php if($cc_dropdown=="SCE") echo "selected";  ?> ><sub>SCE</sub></option>
                <option value="AG" <?php if($cc_dropdown=="AG") echo "selected";  ?> ><sub>Ag/AgCl</sub></option>
                <option value="MSE" <?php if($cc_dropdown=="MSE") echo "selected";  ?> >MSE</option>
                <option value="CSE" <?php if($cc_dropdown=="CSE") echo "selected";  ?> >CSE</option>
            <?php endif; ?>
            </select>
            <br />
            <?php

if(isset($_GET['sbmt']))
{
        ...

更新2

我忘了提到你还必须为其他下拉列表执行此操作。这仅适用于上层。不过不要担心 - 这是类似的。