2017-02-22 31 views
0

我有一个带有下拉选择列的表。如何在保存按钮上保存选定的选项单击我的数据库。从表中保存多个下拉选择 - PHP MySQL

我尝试以下,但我似乎无法得到任何数据放入数组:

表中显示

Drowdowns In Table

DROPDOWN表:

<td width="10%" nowrap> 

<select class="bs-select form-control" name="providers[]"  id="serviceProviders" data-id="<?php echo $row["provider_id"]; ?>" id="serviceProvider" data-live-search="true" data-size="8">  
</select> 

JavaScript部分

$(document).on('click', '#saveChanges', function(e){ 

      var id = []; 

      $('serviceProviders').each(function(i){ 

       id[i] = $(this).val(); 

        console.log('Service Provider Values are = ' + id[i]); 
      }); 
      if(id.length === 0) //tell you if the array is empty 
      { 
       alert("Please Select at least one transaction"); 
      } 
      else 
      { 
       $.ajax({ 
         url:'some_php_scrip_to_save_the_data.php', 
         method:'POST', 
         data:{id:id}, 
         success:function() 
         { 
          for(var i=0; i<id.length; i++) 
          { 
           // $('tr#'+id[i]+'').css('background-color', '#ccc'); 
           // $('tr#'+id[i]+'').fadeOut('slow'); 

           window.location.reload(true); 

          } 
         } 
       }); 
      } 


    }); 
+0

你应该尽量多选这个。 –

+0

我该怎么做? –

+1

在HTML文档中,ID必须是唯一的,您不能在多个元素上使用相同的ID。 '$('serviceProviders')'会选择tha _tag name_'serviceProviders'的元素,其中没有。 – CBroe

回答

0

元素的ID属性必须是唯一的。 $('serviceProviders')应该只返回一个select元素。尝试将列号后缀加到id中,以使其唯一。

var id = [] 
 
for (var i = 1; i <= MaxColNo; i++) { 
 
    var e = document.getElementById("serviceprovider" + i); 
 
    var text = e.options[e.selectedIndex].text; 
 
    id[i] = text; 
 
}

而且在报价Ajax调用环绕属性名称 - 数据:{ 'ID':ID}