PHP + 表单选择选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3289109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:14:43  来源:igfitidea点击:

PHP + form select options

phpformsselect

提问by sea_1987

I have a select that looks like this, it is written in in HTML and is not rendered via any php,

我有一个看起来像这样的选择,它是用 HTML 编写的,不是通过任何 php 呈现的,

    <select name="position">                                           
<option value="left">Left</option>
    <option value="right">Right</option>
    <option value="centre">Centre</option>
    </select>

The value gets sent to database and later on gets returned from the database as a variable in the form of $v['position'], using this and my original form how can I make the option that matches the varibale the default selection?

该值被发送到数据库,然后从数据库中作为变量以 的形式返回$v['position'],使用这个和我的原始形式如何使与变量匹配的选项成为默认选择?

采纳答案by Ahmed Aman

try this

尝试这个

<select name="position">                                           
    <option value="left" <?php echo $v['position']=='left'?'selected="selected"':'' ?> >Left</option>
    <option value="right" <?php echo $v['position']=='right'?'selected="selected"':'' ?>>Right</option>
    <option value="centre" <?php echo $v['position']=='centre'?'selected="selected"'?:'' >>Centre</option>
</select>

回答by fabrik

You didn't specify when the form displayed again. If immediately when user is submitted the form, you need to insert this snippet to every option:

您没有指定表单何时再次显示。如果在用户提交表单时立即,您需要将此代码段插入到每个选项中:

<option value="left"<?php echo $v['position'] == 'left' ? ' selected' : ''; ?>>Left</option>
<option value="right"<?php echo $v['position'] == 'right' ? ' selected' : ''; ?>>Right</option>
<option value="centre"<?php echo $v['position'] == 'centre' ? ' selected' : ''; ?>>Centre</option>

OR:

或者:

You must iterate through variables via PHP :(

您必须通过 PHP 迭代变量:(

$html = '<select name="position">';

$opts = array('left', 'right', 'centre');
foreach($opts as $option)
{
    $html .= '<option value="' . $option . '"';
    $html .= $option == $v['position'] . ' selected' : '';
    $html .= '>' . ucfirst($option) . '</option>';
}
$html .= '</select>';

print $html;

回答by Felix Kling

You can create the options in a loop and check whether the current element equals the value in $v['position']and set the selectedattribute accordingly.

您可以在循环中创建选项并检查当前元素是否等于中的值$v['position']并相应地设置selected属性。

<?php
$options = array('left'=>'Left', 'right'=>'Right', 'centre'=>'Centre');
?>

<select name="position">      
<?php foreach($options as $value=>$text):?>                                     
    <option value="<?php echo $value ?>" 
            <?php echo ($v['position'] == $value) ? 'selected="selected"' : '' ?> >
            <?php echo $text ?>
    </option>
<?php endforeach; ?>
</select>

回答by Gordon

You can do it with DOMwithout having to touch your HTML. If this is your HTML:

您可以使用DOM来完成,而无需接触您的 HTML。如果这是您的 HTML:

$template = <<< TPL
<select name="position">
    <option value="left">Left</option>
    <option value="right">Right</option>
    <option value="centre">Centre</option>
</select>
TPL;

And this is the value that was selected:

这是选择的值:

$value = 'right';

You can do

你可以做

$dom = new DOMDocument;
$dom->loadXml($template);
$xPath = new DOMXPath($dom);
$node = $xPath->query(sprintf('//option[@value = "%s"]', $value));
if($node->item(0)) {
    $node->item(0)->setAttribute('selected', 'selected');
}
echo $dom->saveXML($dom->documentElement);

And that will output:

这将输出:

<select name="position">
    <option value="left">Left</option>
    <option value="right" selected="selected">Right</option>
    <option value="centre">Centre</option>
</select>

回答by Mark Baker

<select name="position">                                            
    <option value="left"<?php echo ($v['position'] == 'left') ? ' selected="selected" : ''; ?>>Left</option> 
    <option value="right"<?php echo ($v['position'] == 'right') ? ' selected="selected" : ''; ?>>Right</option> 
    <option value="centre"<?php echo ($v['position'] == 'centre') ? ' selected="selected" : ''; ?>>Centre</option> 
</select>