php 如何从下拉列表中回显名称?

发布于 2024-11-06 09:19:27 字数 580 浏览 2 评论 0原文

换句话说,我有一个下拉列表:

<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />

我想要做的是根据它们的值和所选的一个来回显 BackgammonChess 。这是我到目前为止所拥有的。但我得到的是数字而不是名字,

$values = $_POST['gamelist'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit']) && ($_POST['submit'] == 'Submit')) {
echo $values;
}
}

谢谢

in another words, i have an drop-down list:

<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />

what i want to do is to echo out Backgammon or Chess based on their values and on witch one is selected. here is what i have so far. but i get numbers instead of names

$values = $_POST['gamelist'];
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (isset($_POST['submit']) && ($_POST['submit'] == 'Submit')) {
echo $values;
}
}

thanks

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

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

发布评论

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

评论(3

甜尕妞 2024-11-13 09:19:27

您得到数字是因为您在选项值上使用了数字。

<select name="gamelist" id="gamelist">
 <option value="Backgammon">Backgammon</option>
 <option value="Chess">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />

You get numbers because you're using numbers on the value for the options.

<select name="gamelist" id="gamelist">
 <option value="Backgammon">Backgammon</option>
 <option value="Chess">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
泡沫很甜 2024-11-13 09:19:27

$_POST['gamelist'] 中,您应该有“1”或“2”。

要显示它,您应该使用类似于以下内容的内容:

$options = array(
    '1' => 'Backgammon',
    '2' => 'Chess',
);

$value = $_POST['gamelist'];

echo $options[$value]; // will echo proper option, assuming NO multiselect

这绝对应该有效。

仅供参考: 标记中包含的部分不与表单数据一起传递,仅将 value 属性中分配给所选选项的值传递。

In $_POST['gamelist'] you should have '1' or '2'.

To display it, you should use something similar to the following:

$options = array(
    '1' => 'Backgammon',
    '2' => 'Chess',
);

$value = $_POST['gamelist'];

echo $options[$value]; // will echo proper option, assuming NO multiselect

This should definitely work.

FYI: The part contained within the <option> tag is not being passed with form data, only the values assigned within value attributes to chosen options.

独自←快乐 2024-11-13 09:19:27

提交时仅发送值,而不发送文本。如果此下拉列表来自数据库,您可以使用数据库根据 ID 获取标题。如果这是一个固定列表,则使用像这样的数组

$list = array ( 1 = > 'Backgammon' , 2 => 'Chess');
$title = $list[$_POST['gamelist']];

这只是一个示例,根据您的情况进行调整。

On submit only the value is sent, not the text. If this drop down is coming from the database, you can use the DB to get the title against the ID. If this is a fixed list, then use an array like

$list = array ( 1 = > 'Backgammon' , 2 => 'Chess');
$title = $list[$_POST['gamelist']];

This is just an example, adjust it as per your situation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文