Javascript 如何在数据库中插入JSON数组

Javascript 如何在数据库中插入JSON数组,javascript,php,jquery,mysql,json,Javascript,Php,Jquery,Mysql,Json,当选择第一个下拉菜单时,我使用下拉菜单,根据选择的第一个下拉菜单,它将显示第二个下拉菜单。每个下拉列表都有来自数据库的数据,问题是我想根据选择的下拉列表将其插入数据库的新表中 所有代码都在同一页上,这是php <?php $sql= mysql_query("SELECT KodeMapel,NamaTema FROM mapel"); while ($row = mysql_fetch_array($sql)) {

当选择第一个下拉菜单时,我使用下拉菜单,根据选择的第一个下拉菜单,它将显示第二个下拉菜单。每个下拉列表都有来自数据库的数据,问题是我想根据选择的下拉列表将其插入数据库的新表中

所有代码都在同一页上,这是php

<?php
            $sql= mysql_query("SELECT KodeMapel,NamaTema FROM mapel");
            while ($row = mysql_fetch_array($sql))
            {
                $tema[] = array("KodeMapel" => $row['KodeMapel'], "val" => $row['NamaTema']);
            }

            $query = mysql_query("SELECT KodeMapel, Subtema FROM subtema");

            while ($row = mysql_fetch_array($query))
            {
                $subtema[$row['KodeMapel']][] = array("KodeMapel" => $row['KodeMapel'], "val" => $row['Subtema']);
            }

            $jsonTema = json_encode($tema);
            $jsonSubTema = json_encode($subtema);
        ?>
这是表单,包含javascript。javascript中有php代码

<script type='text/javascript'>
  <?php
    echo "var tema = $jsonTema;";
    echo "var subtema = $jsonSubTema;";
  ?>
  function loadtema(){
    var select = document.getElementById("PilihTema");
    select.onchange = updateSubTema;
    for(var i = 0; i < tema.length; i++){
      select.options[i] = new Option(tema[i].val,tema[i].KodeMapel);          
    }
  }
  function updateSubTema(){
    var PilihTema = this;
    var idtema = this.value;
    var PilihSubtema = document.getElementById("PilihSubtema");
    PilihSubtema.options.length = 0; //delete all options if any present
    for(var i = 0; i < subtema[idtema].length; i++){
      PilihSubtema.options[i] = new Option(subtema[idtema][i].val,subtema[idtema][i].KodeMapel);
    }
  }
</script>
<body onload='loadtema()'>
<select id='PilihTema' name='PilihTema' class='form-control11'>
</select>

<select id='PilihSubtema' name='PilihSubtema' class='form-control11'>
</select>
<button input class="btn btn-success" id="submit" type="submit" name="add" value="Simpan" onclick="return(submitmapel());"/>
    <i class="fa fa-save fa-fw"></i> Simpan

到目前为止,我在您的代码中看到一些错误

select.onchange = updateSubTema;
您缺少该功能。应为select.onchange=updateSubTema

我不确定var PilihTema是否=这个;实际上,我会选择任何东西,我个人会按照下面的方式来做

loadTema函数:

var select = document.getElementById("PilihTema");
select.onchange = updateSubTema(select.value);
updateTema函数

function updateTema(pilihTema){
  //Your code here
}
编辑:如果您的意思是希望在for循环中每次运行时添加一个项,我建议您使用jQuery,因为我认为这样做更容易:

$(document).ready(function(){
  <?php
    echo "var tema = $jsonTema;";
    echo "var subtema = $jsonSubTema;";
  ?>

  //Load Tema
  $.each(tema, function (i, item) {
  //I am unsure as to what you mean by KodeMapel, though if this is associative arrays encoded as JSON, you will want to use item[val] and item[KodeMapel]
    $("#PilihTema").append(new Option(item.val, item.KodeMapel));
  });

  //Called when the first select field is changed.
  $("#PilihTema").change(function(){
    //Get value of first select fields' selected item
    var pilihValue = $("#PilihTema option:checked").val();

    //Remove all the options from the second select field
    $("#PilihSubtema").html(""); 

    //For each subtema, with first select fields value, add option to second select field
    $.each(subtema[pilihValue], function (i, item) {
      //Again, I am unsure as to what you mean by KodeMapel, though if this is associative arrays encoded as JSON, you will want to use item[val] and item[KodeMapel]
      $("#PilihSubtema").append(new Option(item.val, item.KodeMapel));
    });

  });

});

@好的,不用担心,移除。我觉得这没用。谢谢你的回复。当我尝试添加和删除var this时,组合框不显示任何内容。@graybarket171 By combobox,您是指选择下拉列表吗?