[英]PHP + form select options
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']
的形式从数据库返回,使用此原始表单如何使该变量与varibale匹配为默认选择?
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;
You can create the options in a loop and check whether the current element equals the value in $v['position']
and set the selected
attribute 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>
You can do it with DOM without having to touch your HTML. 您无需触摸HTML就可以使用DOM 。 If this is your 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>
<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>
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.