PHP:下拉框中的选定值

PHP:下拉框中的选定值

问题描述:

I am doing an dropdown box, where I have let's say we have "option1" and "option2", the dropbox selects(marks) the "option1" and then we select the "option2", I want it to mark the "option2" in the dropdown box, but it doesn't mark it.

I'm trying to avoid to do javascript on this one, so I wonder if I can do it only on PHP.

Advices? Thanks!

Edit. Well the problem is that i dont know how many options i have before building the dropdown menu. I get an array generated from a table in a database and make the options based on that. Code:

<select name="department">
            <?php foreach(bloggModelControler::getDepartments($_SESSION['user']) as $tempDepartment){
                if(strcmp($tempDepartment, $department) == 0){
                    $selected = ".selected='selected'.";
                }else{
                    $selected = ".selected=''.";
                }
                $dropdown = "<option \"$selected\" value=\"$tempDepartment\">\"$tempDepartment\" Selected</option>";
                echo $dropdown;
}?>         
</select>

and $department:

<?php
        if(isset($_POST['department'])){
            $department = $_POST['department'];
        }else{
            $departments = bloggModelControler::getDepartments($_SESSION['user']);
            $department = $departments[0];
        }
    ?>

我正在做一个下拉框,让我们说我们有“option1”和“option2”,这是一个dropbox 选择(标记)“option1”然后我们选择“option2”,我希望它在下拉框中标记“option2”,但它没有标记它。 p>

我试图避免在这个上做javascript,所以我想知道我是否只能在PHP上做。 p>

建议? 谢谢! p>

编辑。 问题是我在构建下拉菜单之前不知道我有多少选项。 我得到一个从数据库中的表生成的数组,并根据它生成选项。 编码: p>

 &lt; select name =“department”&gt; 
&lt;  ?php foreach(bloggModelControler :: getDepartments($ _ SESSION ['user'])as $ tempDepartment){
 if(strcmp($ tempDepartment,$ department)== 0){
 $ selected =“。select ='selected  '。;; 
} else {
 $ selected =“。select =''。”; 
} 
 $ dropdown =“&lt; option \”$ selected \“value = \”$ tempDepartment \“&gt  ; \“$ tempDepartment \”Selected&lt; / option&gt;“; 
 echo $ dropdown; 
}?&gt;  
&lt; / select&gt; 
  code>  pre> 
 
 

和$ department: p>

 &lt;?php 
 if(isset)  ($ _POST ['department'])){
 $ department = $ _POST ['department']; 
} else {
 $ departments = bloggModelControler :: getDepartments($ _ SESSION ['user']); 
  $ department = $ departments [0]; 
} 
?&gt; 
  code>  pre> 
  div>

Updated answer based on updated question (included code)

Don't put the periods inside the text variables for 'selected'.

if(strcmp($tempDepartment, $department) == 0){
                $selected = "selected='selected'";
            }else{
                $selected = "selected=''";
            }

Previous answer

In your PHP code that generates the HTML for the Select box, you have to specify which option is selected.

For example:

<select name="selectbox">
  <option <?php if ($_POST['selectbox'] == 'option1') echo 'selected="selected"';?>>option1</option>
  <option <?php if ($_POST['selectbox'] == 'option2') echo 'selected="selected"';?>>option2</option>
</selected>

Alternate syntax:

<select name="selectbox">
  <option <?= ($_POST['selectbox'] == 'option1')? 'selected="selected"' : '';?>>option1</option>
  <option <?= ($_POST['selectbox'] == 'option2')? 'selected="selected"' : '';?>>option2</option>
</selected>