javascript - 如何将 JSON 数组插入数据库

标签 javascript php jquery mysql json

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

所有代码都在同一个页面上,这就是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);

更新Tema函数

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

  });

});

关于javascript - 如何将 JSON 数组插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43967135/

相关文章:

javascript - Jquery 循环 Json 数组时仅附加最后一个值

jquery - 为什么disable_with不适用于rails form_for?

javascript - jQuery(div#id) 对无效 html 的行为

javascript - 如何在 webview 中加载 Activity 的 html

PHP 生成一个空白网页

php - 定时器 mysql php 检查

php - 标准类对象到数组

javascript - 将 Highchart 时间数据分组为周和月

javascript - 如何清除html中的pre标签

javascript - 理解 AngularJS 中的 Controller 范围