2016-07-04 113 views
0

任何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> 

奇怪的是,当我点击“转换”的下拉菜单回去什么“选择”的HTML变量了。我能做些什么来解决这个问题?

感谢 -Lorelo

+0

你不需要了解Referanse电极是什么, – Lorelo

+1

你拼错在第二种方式中“引用”现在,只是说... –

+0

哦,谢谢......我现在感觉不好 – Lorelo

回答

2

OK,我不知道这只是我的服务器上的问题,但是当我试图让未定义的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”属性。

UPDATE

这里就是我所做的:

... 
    <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

我忘了提,还必须对其他下拉列表中做到这一点。这只是上面的一个。不要担心,但是 - 这是类似的。

+0

才想起来像货币为你。我没有错误。 – Lorelo

+0

你使用PHP 7或5吗? – Lorelo

+0

这是答案,正在处理同一件事 – Nick