如何将JSON数组插入数据库

时间:2017-05-14 17:53:51

标签: 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

1 个答案:

答案 0 :(得分:0)

到目前为止,我发现代码中存在一些错误。

select.onchange = updateSubTema();

你缺少()函数。应为var PilihTema = this;

我不确定var select = document.getElementById("PilihTema"); select.onchange = updateSubTema(select.value); 是否会实际选择任何内容,我个人会按照以下方式进行操作。

loadTema函数:

function updateTema(pilihTema){
  //Your code here
}

updateTema函数

for

编辑:如果你的意思是想要通过$(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)); }); }); }); 循环每次运行添加一个项目,我建议按以下方式进行操作(我使用jQuery,因为在我看来它更容易):

var foo = null;
(foo) = "bar";