![](/img/trans.png)
[英]Magento edit form fieldset - get value of select dropdown into a label
[英]php edit form, get dropdown to pull value for that record
我有2種表單,一種是我輸入記錄的表單,另一種是我編輯記錄的表單。
在添加記錄的表單上,我有一個下拉列表,可查詢數據庫中另一個表的值。
當我單擊“編輯”記錄表單來編輯此數據時,我無法獲得已經輸入的值,以便在下拉列表中進行預選擇。
<select name="aankomstluchthaven">
<option name="aankomstluchthaven" value="">--Select--</option>
<?php
$list=mysql_query("select luchthavenID, luchthavencode from tbl_luchthaven
order by luchthavencode ASC");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?echo $row_list['luchthavenID']; ?>"> <?echo
$row_list['luchthavencode']; ?> </option>
<?
}
?>
</select>
正在更新的字段是“ aankomstluchthaven”。 因此,應該填充存儲表格數據的表中的值(tbl_vluchtgegevens)。 其中tbl_luchthaven.luchthavenID = tbl_vluchtgegevens.aankomstluchthaven
我嘗試輸入選項值,但這不是我想要的。
編輯以包含帶有下拉列表的示例php頁面(請注意,我已從上面的示例中的aankomstluchthaven更改為下面的示例中的vertrekluchthaven。同樣適用。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>bijwerk vluchtgegevens Form</title>
</head>
<body>
<? include "datalogin.php";//database connection
$order = "SELECT *
FROM tbl_vluchtgegevens
WHERE gegevenID='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<table border=1>
<tr>
<td width="547" align=center>bijwerk vluchtgegevens: <br>
gegevenID = <? echo "$row[gegevenID]"?></td>
<td width="547" align=center> </td>
</tr>
<tr>
<td>
<table>
<form method="post" action="bijwerkvlucht_post.php">
<input type="hidden" name="id" value="<? echo "$row[gegevenID]"?>">
<tr>
<td width="208">Vertrekluchthaven </td>
<td width="325">
<select name="vertrekluchthaven">
<option name="vertrekluchthaven" value="">--Select--</option>
<?php
$list=mysql_query("select luchthavenID, luchthavencode from tbl_luchthaven order by luchthavencode ASC");
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<?echo $row_list['luchthavenID']; ?>"> <?echo
$row_list['luchthavencode']; ?> </option>
<?
}
?>
</select>
</td>
</tr><tr>
<td align="right">
<input type="submit"
name="submit" id="submit" value="Submit"> </td>
</tr>
</form>
</table> </td>
<td> </td>
</tr>
</table>
<?php
// close connection
mysql_close();
?>
</body>
</html>
假設您在編輯表單時通過id或其他方式獲取值,則將執行以下操作:
<option value="<?echo $row_list['luchthavenID']; ?>" <? echo ($row['luchthavenID'] == $row_list['luchthavenID'] ? ' selected="selected"' : ''); ?>> <?echo $row_list['luchthavencode']; ?> </option>
每次通過循環,您都將對照查詢中提取的值檢查該值。 如果它們匹配,請選中此選項。
更新我仍然不知道您的字段叫什么,但是假設它是$row['luchthavenID']
我會這樣寫:
echo '<option value="'.$row_list['luchthavenID'].'"'.($row['luchthavenID'] == $row_list['luchthavenID'] ? ' selected="selected"' : '').'>'.$row_list['luchthavencode'].'</option>';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.