由於標題暗示我需要我的下拉框在用戶點擊提交後保留其值,並且不會將其設置回該值。我已經上傳了我的整個代碼,以便更好地瞭解問題所在,因爲我使用的是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>";
?>
任何幫助將深表感謝!提前致謝。
小費:在未來,儘量不封裝在PHP中的HTML。這種方法創建混亂,容易出錯的代碼。 – 2013-04-26 19:13:50
感謝您的提示。這是正常工作後,我會創建一個HTML頁面,而不是這種方式的PHP引用。 – 2013-04-26 19:16:01
請不要在新的應用程序中使用**'mysql_query'。此接口很危險,不推薦使用,並且將在未來的PHP版本中被刪除。面向未來的替代品是[PDO](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/),其優點是[從SQL注入漏洞](http://bobby-tables.com/)提供可靠的保護。有沒有什麼理由你這樣做PHP而不是使用[適當的框架](http://www.phpframeworks.com/top-10-php-frameworks/)?你爲自己創造了很多工作。 – tadman 2013-04-26 19:16:38