Select 默认值来自 GET 值
Select default value from GET value
我的网站使用表单编辑来单击一条记录,然后打开一个表单并允许编辑所有值,并保存以附加新值。然而,我面临的问题是 select,它应该使用 $_GET 中的默认值,但默认值是按字母顺序排在第一位的值,而不是它实际应该是的值。所以问题是,如果我编辑并保存,表单会自动在 select 框中放置错误的值。
我在 select 中使用的代码如下:
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";}
echo "</select><br>"; //CLOSE DROP DOWN BOX
如何编辑上面的代码片段以允许我的 $cli = $_GET["ClientCode"] 在 select 框中显示为页面加载时的默认值
按以下方式更改您的 <select ...> ... <select>
下拉列表代码,
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
$output = "<option value='" . $row['ClientCode'] ."'";
if(isset($_GET["ClientCode"]) && $row['ClientCode'] == $_GET["ClientCode"]){
$output .= " selected='selected'";
}
$output .= ">" . $row['ClientName'] ."</option>";
echo $output;
}
echo "</select><br>";
您需要在循环中检查 $_GET 并为需要的选项执行 selected="selected"
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
if (!empty($_GET["ClientCode") && $_GET["CleintCode"] == $row["ClientCode"]))
{
echo "<option selected='selected' value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
else
{
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
}
echo "</select><br>"
将您的 while 循环更改为:
$cli = isset($_GET["ClientCode"])?$_GET["ClientCode"]:"";
while ($row = mysql_fetch_array($result)) {
$selected = ($cli == $row['ClientCode']) ? "selected" : "";
echo "<option value='" . $row['ClientCode'] ."' $selected >" . $row['ClientName'] ."</option>";}
使用这个
if($row['ClientCode'] == $_GET["ClientCode"])
{
?>
<option selected="selected">
Value goes here...
</option>
<?php
}
我的网站使用表单编辑来单击一条记录,然后打开一个表单并允许编辑所有值,并保存以附加新值。然而,我面临的问题是 select,它应该使用 $_GET 中的默认值,但默认值是按字母顺序排在第一位的值,而不是它实际应该是的值。所以问题是,如果我编辑并保存,表单会自动在 select 框中放置错误的值。
我在 select 中使用的代码如下:
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."</option>";}
echo "</select><br>"; //CLOSE DROP DOWN BOX
如何编辑上面的代码片段以允许我的 $cli = $_GET["ClientCode"] 在 select 框中显示为页面加载时的默认值
按以下方式更改您的 <select ...> ... <select>
下拉列表代码,
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
$output = "<option value='" . $row['ClientCode'] ."'";
if(isset($_GET["ClientCode"]) && $row['ClientCode'] == $_GET["ClientCode"]){
$output .= " selected='selected'";
}
$output .= ">" . $row['ClientName'] ."</option>";
echo $output;
}
echo "</select><br>";
您需要在循环中检查 $_GET 并为需要的选项执行 selected="selected"
echo "<select name='ClientCode'>";
while ($row = mysql_fetch_array($result)) {
if (!empty($_GET["ClientCode") && $_GET["CleintCode"] == $row["ClientCode"]))
{
echo "<option selected='selected' value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
else
{
echo "<option value='" . $row['ClientCode'] ."'>" . $row['ClientName'] ."
</option>";
}
}
echo "</select><br>"
将您的 while 循环更改为:
$cli = isset($_GET["ClientCode"])?$_GET["ClientCode"]:"";
while ($row = mysql_fetch_array($result)) {
$selected = ($cli == $row['ClientCode']) ? "selected" : "";
echo "<option value='" . $row['ClientCode'] ."' $selected >" . $row['ClientName'] ."</option>";}
使用这个
if($row['ClientCode'] == $_GET["ClientCode"])
{
?>
<option selected="selected">
Value goes here...
</option>
<?php
}