簡體   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