twitter-bootstrap 在 php 中获取 bootstrap 下拉菜单的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38900832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get values of bootstrap dropdown in php
提问by Somename
I have created a form with multiple fields like input type name, checkboxes and also a dropdown. My code for dropdown:
我创建了一个包含多个字段的表单,例如输入类型名称、复选框和下拉列表。我的下拉代码:
<div class="container">
<form action="selecttest.php" method="post">
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<input type="submit" name ="submit"/>
I want to get the value of the selected <li>in the selecttest.php. The following code is not working:
我想<li>在selecttest.php 中获取selected 的值。以下代码不起作用:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
How can I get the value of the dropdown in the php page? There are other elements in the form as mentioned above so i cannot use jquery onclick event. And this is a non ajax form.
如何获取php页面中下拉菜单的值?如上所述,表单中还有其他元素,因此我无法使用 jquery onclick 事件。这是一个非 ajax 形式。
Thanks.
谢谢。
回答by Ben
A dropdown menu is not the same as a selectinput.
下拉菜单与select输入不同。
Within a form, the syntax for a selectinput (like yours, within Bootstrap) would be:
在表单中,select输入的语法(如你的,在 Bootstrap 中)将是:
<label for="select_1">Select list:</label>
<select class="form-control" id="select_1" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
So long as the selecthas an attribute namewith value "thenumbers", and the selectis within the form that is being submitted, you will be able to get the value of the selected element in PHP with:
只要select有一个name值为“thenumbers”的属性,并且在select提交的表单中,您就可以通过以下方式在 PHP 中获取所选元素的值:
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
To use the Bootstrap dropdown as a faux select, you can use JavaScript. Here is an example with jQuery:
要将 Bootstrap 下拉列表用作假select,您可以使用 JavaScript。下面是一个 jQuery 的例子:
HTML
HTML
<!-- Create dropdown and add specific class '.thenumbers' to ul element -->
<div class="dropdown">
<div class="dropdown-toggle somename1" data-toggle="dropdown"id="menu1"></div>
<ul class="dropdown-menu myclass numberom1 thenumbers" aria-labelledby="menu1" name="thenumbers">
<li value="one">One</li>
<li value="two">Two</li>
<li value="three">Three</li>
<li value="four">Four</li>
<li value="five">Five</li>
</ul>
</div>
<!-- Create a hidden input -->
<input type='hidden' name='thenumbers'>
jQuery
jQuery
$(function(){
//Listen for a click on any of the dropdown items
$(".thenumbers li").click(function(){
//Get the value
var value = $(this).attr("value");
//Put the retrieved value into the hidden input
$("input[name='thenumbers']").val(value);
});
});
That way (as long as the hidden input is within the form), when the form is submitted, the "thenumbers" input value will be the last selected dropdown option.
这样(只要隐藏输入在表单内),当表单被提交时,“thenumbers”输入值将是最后选择的下拉选项。
回答by Syed Muneeb Ul Hasan
Your form is not submitting because there is no submit button.Try this code.
您的表单没有提交,因为没有提交按钮。试试这个代码。
HTML CODE
HTML代码
<form action="selecttest.php" method="post">
<div class="dropdown" >
<select class="form-control" name="thenumbers">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
</select>
</div>
<input type="submit" name ="send"/>
</form>
PHP CODE
PHP代码
if(isset($_POST['send']))
{
$val = $_POST["thenumbers"];
echo "the Value selected is ".$val;
}
回答by Alex Maina
When using Input group-bootstrap, you can get values in the PHP page by writing your code like this:
使用Input group-bootstrap 时,您可以通过编写如下代码来获取 PHP 页面中的值:
HTML/Bootstrap
HTML/引导程序
<select name = "thenumbers" class="btn btn-outline-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<option value ="one" name ="one">One</option>
<option value ="two" name ="two">Two</option>
<option value ="three" name ="three">Three</option>
</select>
<input class="form-control border-secondary py-2" name="searchterm" type="text" >
<div class="input-group-append">
<button class="btn btn-primary" name="submit" type="submit" value ="su bmit">
<i class="fa fa-search"></i>
</button>
</div>
PHP code:
PHP代码:
<?php
if(isset($_POST['submit']) && !empty($_POST['searchterm'])){
if(isset($_POST['thenumbers']){
$val = $_POST['thenumbers'];
echo $val;
}
}

