由于标题暗示我需要我的下拉框在用户点击提交后保留其值,并且不会将其设置回该值。我已经上传了我的整个代码,以便更好地了解问题所在,因为我使用的是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