1 回答

TA貢獻1804條經驗 獲得超3個贊
可能這一行只需要這樣做:
$selected = (!empty($_POST['fund']) && $_POST['fund'] == $row['FUND'])) ? 'selected' : '';
您所擁有的是將默認選項與您實際從下拉列表中選擇的選項相匹配 || ($row['FUND'] == '--Select A Fund--'),并且可能還添加到該默認選項中。selected在瀏覽器中查看頁面源代碼,該selected屬性可能有兩個選項。
默認情況下,如果未選擇任何內容,它只會顯示下拉列表中的第一項--Select A Fund--。
另外,您可能應該在$selected變量之前和引號之后有一個空格:
echo '<option value="' . $row['FUND'] . '" ' . $selected . '>' . $row['FUND'] . '</option>';
應該出來:
<option value="whatever" selected>Whatever</option>
編輯
根據您的編輯,您需要從提交按鈕中刪除名稱屬性,或對其進行重命名。這和你的select名字有沖突。
<input type="submit" value="Show Current Fund Investors"/>
一個提示:
您應該將對該列表的獲取封裝在一個函數中(至少)并將其包含在頂部的頁面中:
/functions/fetchFundAccounts.php
<?php
function fetchFundAccounts($conn)
{
$stid = oci_parse($conn, 'SELECT Account_name ||\' - \'|| Fund_id as FUND, FUND_ID FROM FUND_ACCOUNTS');
$success = oci_execute($stid);
$results = [];
while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
$results[] = $row;
}
return $results;
}
使用方法:
<?php include(__DIR__.'/functions/fetchFundAccounts.php') ?>
<form name= "fund" method="post" >
<label id= "fund" for="fund">Fund:</label>
<select name="fund" id="fund">
<option value="--Select A Fund--">--Select a Fund--</option>
<?php foreach(fetchFundAccounts($conn) as $row): ?>
<option value="<?php echo $row['FUND'] ?>" <?php echo (!empty($_POST['fund']) && $_POST['fund'] == $row['FUND']) ? 'selected' : '' ?>><?php echo $row['FUND'] ?></option>
<?php endforeach ?>
</select>
<input type="submit" value="Show Current Fund Investors"/>
</form>
- 1 回答
- 0 關注
- 121 瀏覽
添加回答
舉報