2013-04-26 99 views
0

由於標題暗示我需要我的下拉框在用戶點擊提交後保留其值,並且不會將其設置回該值。我已經上傳了我的整個代碼,以便更好地瞭解問題所在,因爲我使用的是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>"; 

?> 

任何幫助將深表感謝!提前致謝。

+0

小費:在未來,儘量不封裝在PHP中的HTML。這種方法創建混亂,容易出錯的代碼。 – 2013-04-26 19:13:50

+1

感謝您的提示。這是正常工作後,我會創建一個HTML頁面,而不是這種方式的PHP引用。 – 2013-04-26 19:16:01

+0

請不要在新的應用程序中使用**'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

回答

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>"; 
+0

這工作完美!非常感謝!!如果我想爲兩個文本框做同樣的事情,這是否是同一個概念? – 2013-04-26 19:26:00

+0

在textarea中你必須設置內部html。例如,''如果您正在討論input type =「text」,您必須這樣做:'」/>' – jraede 2013-04-27 20:18:35