如何在下拉框中设置所选项目

时间:2022-05-10 07:08:53

Is there any way to set the selected item in a drop down box using the following 'type' code?

有没有办法使用以下“类型”代码在下拉框中设置所选项?

<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>

The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?

数据库持有一个月......我想允许在编辑页面上,他们选择这个月..但它预先填充了他们当前的设置?

9 个解决方案

#1


48  

You need to set the selected attribute of the correct option tag:

您需要设置正确选项标记的selected属性:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

您的PHP看起来像这样:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.

我通常发现创建一个值数组并在其中循环以创建下拉列表更为全面。

#2


15  

You mark the selected item on the <option> tag, not the <select> tag.

您在

So your code should read something like this:

所以你的代码应该是这样的:

<select>
    <option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
    <option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
    ...
    ...
    <option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>

You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.

您可以通过将所有月份名称放在一个数组中并使用基本foreach来减少重复性。

#3


5  

You can use this method if you use a MySQL database:

如果使用MySQL数据库,则可以使用此方法:

include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
    if ($_GET['to'] == $row['id'])
    {
        $selected = 'selected="selected"';
    }
    else
    {
    $selected = '';
    }
    echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);

It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...

它将比较$ _GET ['to']中的用户是否与表中的$ row ['id']相同,如果是,则将创建$ selected。这是一个私人消息系统......

#4


3  

Simple and easy to understand example by using ternary operators to set selected value in php

通过使用三元运算符在php中设置选定值,简单易懂的示例

<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
  <option value="<?php echo $id;?>" <?php echo ($id==  '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>

#5


1  

Simple way

简单的方法

<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">

<option value=""><?php echo "Select"; ?></option>

<?php  foreach ($dropdowndetails as $dropdowndetails) { ?>
    <option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>

#6


1  

Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.

它太旧了但我必须添加我的方式:)因为它是通用的,特别是当你使用静态下拉值时。

function selectdCheck($value1,$value2)
   {
     if ($value1 == $value2) 
     {
      echo 'selected="selected"';
     } else 
     {
       echo '';
     }
     return;
   }

and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns

在你的下拉选项中你可以像这样使用这个功能,你可以尽可能多地使用它,因为它适合你所有的选择框/下拉菜单

<option <?php selectdCheck($row[month],january); ?> value="january">january</option>

:) I hope this function help others

:)我希望这个功能能帮助别人

#7


0  

This is the solution that I came up with...

这是我提出的解决方案......

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">   
    <select name="select_month">
        <?php
            if (isset($_POST['select_month'])) {
                if($_POST["select_month"] == "January"){
                    echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
                }
                elseif($_POST["select_month"] == "February"){
                    echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
                }
            }
            else{
                echo '<option value="January">January</option><option value="February">February</option>';
            }
        ?>
    </select>
    <input name="submit_button" type="submit" value="Search Month">
</form>

#8


0  

A Simple Solution: It work's for me

一个简单的解决方案:它对我有用

<div class="form-group">
    <label for="mcategory">Select Category</label>
    <select class="form-control" id="mcategory" name="mcategory" required>
        <option value="">Please select category</option>
        <?php foreach ($result_cat as $result): ?>
        <option value="<?php echo $result['name'];?>"<?php 
           if($result['name']==$mcategory){
               echo 'selected';
           } ?>><?php echo $result['name']; ?></option>
                                        }
        <?php endforeach; ?>
    </select>
</div>

#9


-4  

You can try this after select tag:

你可以在select标签后试试这个:

<option value="yes" selected>yes</option>
<option value="no">no</option>

#1


48  

You need to set the selected attribute of the correct option tag:

您需要设置正确选项标记的selected属性:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

您的PHP看起来像这样:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.

我通常发现创建一个值数组并在其中循环以创建下拉列表更为全面。

#2


15  

You mark the selected item on the <option> tag, not the <select> tag.

您在

So your code should read something like this:

所以你的代码应该是这样的:

<select>
    <option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
    <option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
    ...
    ...
    <option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>

You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.

您可以通过将所有月份名称放在一个数组中并使用基本foreach来减少重复性。

#3


5  

You can use this method if you use a MySQL database:

如果使用MySQL数据库,则可以使用此方法:

include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
    if ($_GET['to'] == $row['id'])
    {
        $selected = 'selected="selected"';
    }
    else
    {
    $selected = '';
    }
    echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);

It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...

它将比较$ _GET ['to']中的用户是否与表中的$ row ['id']相同,如果是,则将创建$ selected。这是一个私人消息系统......

#4


3  

Simple and easy to understand example by using ternary operators to set selected value in php

通过使用三元运算符在php中设置选定值,简单易懂的示例

<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
  <option value="<?php echo $id;?>" <?php echo ($id==  '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>

#5


1  

Simple way

简单的方法

<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">

<option value=""><?php echo "Select"; ?></option>

<?php  foreach ($dropdowndetails as $dropdowndetails) { ?>
    <option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>

#6


1  

Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.

它太旧了但我必须添加我的方式:)因为它是通用的,特别是当你使用静态下拉值时。

function selectdCheck($value1,$value2)
   {
     if ($value1 == $value2) 
     {
      echo 'selected="selected"';
     } else 
     {
       echo '';
     }
     return;
   }

and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns

在你的下拉选项中你可以像这样使用这个功能,你可以尽可能多地使用它,因为它适合你所有的选择框/下拉菜单

<option <?php selectdCheck($row[month],january); ?> value="january">january</option>

:) I hope this function help others

:)我希望这个功能能帮助别人

#7


0  

This is the solution that I came up with...

这是我提出的解决方案......

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">   
    <select name="select_month">
        <?php
            if (isset($_POST['select_month'])) {
                if($_POST["select_month"] == "January"){
                    echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
                }
                elseif($_POST["select_month"] == "February"){
                    echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
                }
            }
            else{
                echo '<option value="January">January</option><option value="February">February</option>';
            }
        ?>
    </select>
    <input name="submit_button" type="submit" value="Search Month">
</form>

#8


0  

A Simple Solution: It work's for me

一个简单的解决方案:它对我有用

<div class="form-group">
    <label for="mcategory">Select Category</label>
    <select class="form-control" id="mcategory" name="mcategory" required>
        <option value="">Please select category</option>
        <?php foreach ($result_cat as $result): ?>
        <option value="<?php echo $result['name'];?>"<?php 
           if($result['name']==$mcategory){
               echo 'selected';
           } ?>><?php echo $result['name']; ?></option>
                                        }
        <?php endforeach; ?>
    </select>
</div>

#9


-4  

You can try this after select tag:

你可以在select标签后试试这个:

<option value="yes" selected>yes</option>
<option value="no">no</option>