php 如何从下拉列表中回显名称?
换句话说,我有一个下拉列表:
<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" />
我想要做的是根据它们的值和所选的一个来回显 Backgammon
或 Chess
。这是我到目前为止所拥有的。但我得到的是数字而不是名字,
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您得到数字是因为您在选项值上使用了数字。
You get numbers because you're using numbers on the value for the options.
在
$_POST['gamelist']
中,您应该有“1”或“2”。要显示它,您应该使用类似于以下内容的内容:
这绝对应该有效。
仅供参考:
标记中包含的部分不与表单数据一起传递,仅将
value
属性中分配给所选选项的值传递。In
$_POST['gamelist']
you should have '1' or '2'.To display it, you should use something similar to the following:
This should definitely work.
FYI: The part contained within the
<option>
tag is not being passed with form data, only the values assigned withinvalue
attributes to chosen options.提交时仅发送值,而不发送文本。如果此下拉列表来自数据库,您可以使用数据库根据 ID 获取标题。如果这是一个固定列表,则使用像这样的数组
这只是一个示例,根据您的情况进行调整。
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
This is just an example, adjust it as per your situation.