PHP +表单选择选项

发布于 2024-09-10 18:05:16 字数 405 浏览 3 评论 0原文

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

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

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

I have a select that looks like this, it is written in in HTML and is not rendered via any 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ι不睡觉的鱼゛ 2024-09-17 18:05:16

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

<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>

或者:

您必须通过 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 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 :(

$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;
羅雙樹 2024-09-17 18:05:16

您可以在循环中创建选项,并检查当前元素是否等于 $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 create the options in a loop and check whether the current element equals the value in $v['position'] and set the selected attribute accordingly.

<?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>
心如狂蝶 2024-09-17 18:05:16

您可以使用 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;

这是所选的值:

$value = 'right';

您可以这样做

$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);

这将输出:

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

You can do it with DOM without having to touch your HTML. If this is your 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>
本宫微胖 2024-09-17 18:05:16

试试这个

<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>
想挽留 2024-09-17 18:05:16
<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> 
<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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文