如何在PHP文件中设置默认值

时间:2018-10-29 02:41:57

标签: php html-select

我有这个代码:

<select class='okin' name='netiotitmn' id='netiotitmi' disabled>
                                    <option> </option>
                                    <?php
                                        $x = 0; $y = 60;
                                        while($x <= $y) {
                                            $selected="";
                                            if ($x=9)      {$selected="selected";}
                                            echo "<option value='$x'  $selected>" . szpad($x) . "</option>";
                                            $x++;
                                        }
                                    ?>
                                </select>

我希望将值9设置为默认值,但不能更改所有内容。假定值为09,因为如果打开此表单,我会将其设置为默认值,并且如果用户选择其他值,则可以更改该值,但默认情况下应为9。它只是空白,并具有下拉循环。

2 个答案:

答案 0 :(得分:1)

请尝试使用以下代码:

if ($x == 9)      { $selected = " selected"; }

我更改了以下代码:

  1. $ x == 9而不是$ x = 9
  2. {$ selected =“ selected”; }而不是{$ selected =“ selected”; }

它有空间:

“已选择” =>“已选择”

然后替换以下代码:

echo "<option value='9'".$selected.">" . szpad($x) . "</option>";

答案 1 :(得分:0)

永远不要在if子句中设置变量。如果要检查$x是否等于9,则必须使用:

if($x == 9) { ... }

如果要将所有选择选项都设置为9,则可以在代码的这一部分中做到这一点:

echo "<option value='9'  $selected>" . szpad($x) . "</option>";