<select>标记向数据库返回一个空值</select>

时间:2014-02-26 10:31:58

标签: javascript php jquery twitter-bootstrap

所以我试图创建一个循环遍历$ countries的下拉列表,然后将所选国家/地区写入用户表下数据库中的$ country。我得到了下拉列表,但它返回一个空值到数据库(至少是验证)。

以下是对下拉列表的调用

       <div class="form-group col-lg-6 col-sm-12">
        <label for="country">Country</label>
        <div id="countrydd">
          @if(isset($currentCountry->name))
            <select name="country" id="country_display" class="current-value-da-select closed">
                {{(isset($user->country->name))?$user->country->name:$currentCountry->name}}
                @foreach($countries as $country)
                    <option value="{{$country->name}}">{{$country->name}}</option>
                @endforeach
            </select>

          @else
            <select name="country" id="country_display" class="current-value-da-select closed">
                {{(isset($user->country->name))?$user->country->name:'Select your Country'}}
                @foreach($countries as $country)
                    <option value="{{$country->name}}">{{$country->name}}</option>
                @endforeach
            </select>

          @endif

这是下拉列表的Javascript

   //Country Drop Down
$('#countrydd').click(function(e) {
  var selectWrapperCountry = $('#countrydd');
  var currentValueCountry = $(this).find('a').html();
  selectWrapperCountry.find('.current-value-da-select').html(currentValueCountry);
  selectWrapperCountry.find('#country').val(currentValueCountry);
  updateCarretClassCountry();
});
$('#countrydd a.current-value-da-select').click(function() {
  updateCarretClassCountry();
});
function updateCarretClassCountry(){
  if($('#countrydd a.current-value-da-select').hasClass('closed')) {
    $('#countrydd a.current-value-da-select').attr('class', 'current-value-da-select opened');
  }
  else
  {
    $('#countrydd a.current-value-da-select').attr('class', 'current-value-da-select closed');
  }
};

任何有助于获得正确更新价值的帮助都会非常棒。

1 个答案:

答案 0 :(得分:3)

您似乎使用了错误的select标签。选择需要有一个名称(这是你将作为一个post变量得到的),每个选项都需要有一个值。所以它会是这样的:

<select name="country">
  <option value="1">First one</option>
  <option value="2">Second one</option>
</select>

提交表单后,您将拥有

echo $_POST["country"]; //This will output 1 if First one is selected and 2 if Second one is

另外,为什么<a>内有<option>

阅读详情:http://www.w3.org/wiki/HTML/Elements/select