我如何设置< select>的值数据库中的标记? [英] how do i set the value of a <select> tag from the database?

查看:106
本文介绍了我如何设置< select>的值数据库中的标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中选择要在html / php上编辑的条目。我写的代码(从我所学到的)不起作用。我需要一个更好的工作。我搜索谷歌,但我看到的只是把标签的默认值。

  $ civil_status = $ row [8]; //选择数据库的值

< tr>
< td>公民身份:< / td>
< td>< select name =civil_statusvalue ='<?php $ civil_status?>'>
< option value =single>单个< / option>
< option value =已婚>已婚< / option>
< / select>< / td>
< / tr>


解决方案

您可以以快捷方式做到这一点:

 < tr> 
< td>公民身份:< / td>
< td>
< select name =civil_status>
< option value =single<?php print($ civil_status ==single?'selected =selected':); ?>>单,LT; /选项>
< option value =已婚<?php print($ civil_status ==已婚?'selected =已选择':); ?>>已婚< /选项>
< / select>
< / td>
< / tr>

或者您可以编写一个辅助函数来生成整个< option> set:

 函数select_options($ options,$ selected)
{
$ result =;
foreach($ options as $ value => $ label)
{
$ result。='< option value =''。
htmlentities($ value,ENT_QUOTES, UTF-8)。''。
($ selected == $ value?'selected =selected':)。 '>'。
htmlentities($ label,ENT_QUOTES,UTF-8)。 < /选项>;
}
返回$ result;
}

select_options 有两个参数。第一个是关联数组( key =>值,... ),其中数组键是< option> 标签和数组值参数值包含要为每个键显示的标签。



第二个参数指定要在选择框中选择的项目的



函数然后遍历数组并提取标签为每个项目。然后将相应的< option> 标签附加到结果字符串中。它还会在参数上调用 htmlentities 以避免跨站点脚本漏洞。 然后来到kicker:如果它看到一个应该在选择框中选择的项目( $ selected == $ value ),它会自动附加 selected =选择参数到< option> 标记,并选择该元素。



要使用该功能,请执行以下操作:

 < tr> 
< td>公民身份:< / td>
< td>
< select name =civil_status>
print single=>Single,
married=>已婚
) ,$ civil_status));
?>
< / select>
< / td>
< / tr>

您需要创建选择菜单的任何位置都可以更清洁并可重复使用。


i am selecting an entry from the database to be edited on html/php. the code i wrote(from what i've learned) isn't working. i need a better and working one. i've searched google but all i see is only putting the default value for tag.

    $civil_status = $row[8]; //value from selecting the database

    <tr> 
    <td>Civil Status:</td> 
    <td><select name="civil_status" value='<?php $civil_status ?>'>
    <option value="single">Single</option>
    <option value="married">Married</option>
    </select></td>
    </tr>

解决方案

You can do it the quick-and-dirty way:

<tr>
    <td>Civil Status:</td> 
    <td>
        <select name="civil_status">
            <option value="single"<?php print($civil_status == "single" ? ' selected="selected"' : ""); ?>>Single</option>
            <option value="married"<?php print($civil_status == "married" ? ' selected="selected"' : ""); ?>>Married</option>
        </select>
    </td>
</tr>

Or you can write a helper function for generating the entire <option> set:

function select_options($options, $selected)
{
    $result = "";
    foreach($options as $value => $label)
    {
         $result .= '<option value="' . 
             htmlentities($value, ENT_QUOTES, "UTF-8") . '"' . 
             ($selected == $value ? ' selected="selected"' : "") . '>' .
             htmlentities($label, ENT_QUOTES, "UTF-8") . '</option>';
    }
    return $result;
}

select_options takes two parameters. The first is an associative array (key => value, ...) where the array keys are the value parameter values of the individual <option> tags, and the array values contain the labels to display for each key.

The second parameter specifies the value of the item to be selected in the select box.

The function then goes through the array and extracts the value and the label for every item. Then it appends the corresponding <option> tag to the result string. It also calls htmlentities on the parameters in order to avoid cross-site scripting vulnerabilities.

And then comes the kicker: if it sees an item that should be selected in the select box ($selected == $value), it automatically appends the selected="selected" parameter to the <option> tag and thus selects that element.

To use the function, do the following:

<tr>
    <td>Civil Status:</td> 
    <td>
        <select name="civil_status">
            <?php
                  print(select_options(array(
                       "single" => "Single",
                       "married" => "Married"
                  ), $civil_status));
            ?>
        </select>
    </td>
</tr>

Much cleaner and reusable anywhere you need to create a select menu.

这篇关于我如何设置&lt; select&gt;的值数据库中的标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆