如何保存选择下拉列表项的数据库表

问题描述:

我有我可是从我的数据库中获取并在我的PHP文件显示下拉列表如下:如何保存选择下拉列表项的数据库表

<script type="text/JavaScript"> 
//get a reference to the select element 
var $select = $('#bananas'); 

//request the JSON data and parse into the select element 
$.getJSON('http://127.0.0.1/folder/bananas.json', function(data){ 

    //clear the current content of the select 
    $select.html(''); 

    //iterate over the data and append a select option 
    $.each(data.allbananas, function(key, val){ 
    $select.append('<option id="' + val.id + '">' + val.name + '</option>'); 
    }) 
}); 

我的html文件

<select id="bananas" class="form-control"> 
<option value="">-- Select A Banana -- </option></select> 

但我需要用户从表单中选择每个香蕉,当他或她提交时,必须使用php将其保存到不同的表格中。如何查看下拉列表是否来自json文件? 也欢迎Ajax解决方案。

你需要的是: 选择选项更改,更新表?我没有使用下拉list.I一个php文件 试试这个..

<html> 
    <head> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    </head> 
    <body> 
     <select id="bananas"> 
      <option value="1">Option 1</option> 
      <option value="2">Option 2</option> 
      <option value="3">Option 3</option> 
     </select> 
    </body> 
</html> 

<script> 
     $("#bananas").change(function(){ 
      // var option holds the selected option value whenever changed 
     var option = $("#bananas").val(); 
     console.log(option); 

      /* 
      It would be better practice to add the data to a DB like mySQL and then generating a JSON file from there 
      However you can just send this information as you already wanted this way 
      */ 
      $.ajax({ 
       url: './myphpfile.php', 
       data: 
       { 
        option:option 
       } 
      }).done(function(resp){ 
       if(resp == 200) { 
        console.log("Success!"); 
       }else if(resp == 0){ 
        console.log("Failed.."); 
       } 
      }); 
     }); 
</script> 
+0

正在访问一个JSON文件和创建下拉array.The JSON文件中使用PHP file.Hence不知道如何已经创建/ myphpfile.php将适用。请详细说明 –

+0

我在Ajax请求之前做了一个说明,解释了为什么 - “将数据添加到像mySQL这样的数据库然后从那里生成JSON文件是更好的做法 但是,您可以按照您以前所希望的方式发送此信息“您可以将数据从此处发送到JSON文件,而不像您喜欢的那样发送:) – user7071381