选中后保持持久状态的下拉框

时间:2013-04-26 19:07:30

标签: php mysql

正如标题所示,我需要我的下拉框在用户点击提交后保持其值,而不是设置回值。我已经上传了我的整个代码,以便更好地理解这里的问题,因为我正在使用mysql查询来填充下拉列表。

<?php

     $host = "localhost";
     $username = "root";
     $pass = "";
     $database = "database_camcalc";

     $conn = mysql_connect($host, $username, $pass) or die (mysql_error());
     mysql_select_db($database, $conn);

 $query = "SELECT camera FROM camlist";

 $result = mysql_query($query) or die (mysql_error());

 if (isset($_POST['cameraDD']))
 {
  $camSelect = $_POST['cameraDD'];
 }else{
  $camSelect = 'SNV-5200';
 }

 $dropdown = "Select Camera: <select name='cameraDD' value='$camSelect' onchange='this.form.submit()'>";

 while($row = mysql_fetch_assoc($result)) {
  $dropdown .= "\r\n\<option value='{$row['camera']}'>{$row['camera']}</option>";
 }
 $dropdown .= "\r\n</select>";

 $horiQuery = mysql_query("SELECT hori FROM camlist WHERE camera = '$camSelect'");
 if (!$horiQuery) {
   die("Failed: " . mysql_error());
 }
 $hori = mysql_fetch_row($horiQuery);

 $sensorSize = mysql_query("SELECT sensor_size FROM camlist WHERE camera = '$camSelect'");
 if (!$sensorSize) {
   die("Failed: " . mysql_error());
 }

 $sensorQuery = mysql_query("SELECT camlist.width FROM camlist WHERE camlist.camera = '$camSelect'");
 $sensorRow = mysql_fetch_array($sensorQuery);
 $sensorWidth = floatval($sensorRow['width']);


 $horiFOV = 20;
 $distObj = 35;
 echo "<form method='POST'>",
      "Horizontal Field of View: <input type='text' name='horizontalFOV' value='$horiFOV'>",
      "<br />",
      "Distance to Object: <input type='text' name='distToObj' value='$distObj'>",
      "<br />",
      $dropdown,
      "<br />",
      "<input type='submit' name='submit'>",
      "<br />";

      if (isset($_POST['submit']))
      {
        $horiFOV = $_POST['horizontalFOV'];
      }

 $lensCalc = ($distObj / $horiFOV) * $sensorWidth;
 $ppfCalc = $hori[0] / $horiFOV;

 echo "<div style ='float:left; width:100%;'>Selected Camera: $camSelect</div>";
 echo "<div style ='float:left; width:100%;'>Selected FOV: $horiFOV ft</div>";
 echo '<div style ="float:left; width:100%;">',
      'Pixels Per Foot: ',
      round($ppfCalc),
      '</div>';

 if ($ppfCalc < 19){
   echo "Level of detail: Too Low";
 }elseif ($ppfCalc >= 19 and $ppfCalc <=39){
   echo "Level of detail: Observation";
 }elseif ($ppfCalc >=40 and $ppfCalc <=59){
   echo "Level of detail: Forensic Review";
 }elseif ($ppfCalc >=60 and $ppfCalc <=79){
   echo "Level of detail: Identification";
 }elseif ($ppfCalc >=80 and $ppfCalc <=500){
   echo "Level of detail: Fine";
 }

 echo "<div style ='float:left; width:100%;'>Lens focal length: $lensCalc MM</div>";

?>

任何帮助都将深表感谢!提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要检查$camSelect的值是否等于$row['camera']的值。像上面那样设置<select>元素的值不会做任何事情。

如,

$dropdown = "Select Camera: <select name='cameraDD' onchange='this.form.submit()'>";

 while($row = mysql_fetch_assoc($result)) {
    if($row['camera'] == $camSelect) 
        $dropdown .= "\r\n\<option selected='selected' value='{$row['camera']}'>{$row['camera']}</option>";
    else
        $dropdown .= "\r\n\<option value='{$row['camera']}'>{$row['camera']}</option>";
 }
 $dropdown .= "\r\n</select>";