It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
6年前关闭。
我将以下HTML代码直接嵌入一个PHP页面中,
现在,我想根据数据库中的记录选择一个选项,因此在PHP页面中,我可以简单地回显上面的代码,然后选中一个选项(例如,初学者)。但是,我认为这不是正确的方法(因为它涉及很多条件语句来决定要输出的正确代码,即echo),所以我想知道是否有一种方法(如JavaScript / Jquery)为php设置选项值。
6年前关闭。
我将以下HTML代码直接嵌入一个PHP页面中,
<select name="language_levels[]" class="span2">
<option value="beginner">beginner</option>
<option value="intermediate">intermediate</option>
<option value="fluent">fluent</option>
<option value="native">native</option>
</select>
现在,我想根据数据库中的记录选择一个选项,因此在PHP页面中,我可以简单地回显上面的代码,然后选中一个选项(例如,初学者)。但是,我认为这不是正确的方法(因为它涉及很多条件语句来决定要输出的正确代码,即echo),所以我想知道是否有一种方法(如JavaScript / Jquery)为php设置选项值。
最佳答案
$value = 'beginner';
$options = array('beginner', 'intermediate', 'fluent', 'native');
<select name="language_levels[]" class="span2">
<?php foreach($options as $o) {
$selected = $o === $value ? "selected='selected'" : "";
echo "<option value='$o' $selected>$o</option>";
} ?>
</select>