繁体   English   中英

如何将JSON数组插入数据库

[英]How to insert JSON Array into database

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

所有代码都在同一页面上,这就是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 = this; 实际上会选择任何东西,我个人将按照以下方式进行选择。

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));
    });

  });

});

暂无
暂无

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

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