简体   繁体   English

从表中保存多个下拉菜单选择-PHP MySQL

[英]Save Multiple Dropdowns Selections from a Table - PHP MySQL

I have a Table with a column with Drop-down Selections. 我有一个带有下拉菜单的列的表。 How do I save the selected choices on Save Button Click to my database. 如何将“保存”按钮上的选定选项保存到我的数据库中。

I tried the following but I cant seem to get any data into the Array : 我尝试了以下操作,但似乎无法将任何数据放入Array中:

TABLE DISPLAY 桌子展示

表中的下拉菜单

DROPDOWN IN TABLE : 表中的下拉列表:

<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 PART Java脚本部分

$(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); 

                           }  
                      }  
                 });  
            }  


  });  

The ID attribute of an element must be unique. 元素的ID属性必须是唯一的。 $('serviceProviders') should return you only one select element. $('serviceProviders')应该只返回一个选择元素。 Try suffixing the column number to the id inorder to make it unique. 请尝试在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; } 

And in ajax call surround property name with quotes - data :{'id':id} 在ajax中,用引号引起来的环绕属性名称-data:{'id':id}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM