Reputation: 31
As the title suggests I need my dropdown box to keep its value after the user hits submit and not set back to the value. I've uploaded my entire code to better understand what the issue is here since I'm using a mysql query to populate the dropdown.
<?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>";
?>
Any help would be deeply appreciated! Thanks in advance.
Upvotes: 0
Views: 336
Reputation: 6896
You need to check if the value of $camSelect
is equal to the value of $row['camera']
. Setting the value of the <select>
element like you do above does nothing.
E.g.,
$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>";
Upvotes: 1