2017-05-14 3 views
0

Ich benutze Dropdown, wenn ausgewähltes erstes Dropdown-Menü, basierend auf dem ersten Dropdown-Menü wird das zweite Dropdown-Menü angezeigt. Jedes dropdown hat Daten von der Datenbank und das Problem ist, dass ich es in neue Tabelle auf Datenbank einfügen möchte, die auf ausgewähltem Dropdown basiert.So fügen Sie JSON Array in die Datenbank ein

Der gesamte Code auf der gleichen Seite, das ist der 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); 
     ?> 

Dies ist die Form, die Javascript enthält. Innerhalb des Javascript gibt es PHP-Code.

<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 

Antwort

0

Bisher sehe ich einige Fehler in Ihrem Code.

select.onchange = updateSubTema; 

Sie fehlen() für die Funktion. Sollte sein select.onchange = updateSubTema();

Ich bin mir nicht sicher, ob var PilihTema = this; tatsächlich alles auswählen wird, persönlich würde ich es auf die folgende Weise tun.

loadTema Funktion:

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

updateTema Funktion

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

EDIT: Wenn Sie meinen, Sie ein Element pro Lauf durch Ihre for Schleife hinzufügen möchten, würde ich es die folgende Art und Weise schlagen tun (Ich benutze jQuery, wie es viel einfacher ist meiner Meinung nach):

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

    }); 

}); 
+0

@skobaljic okay, keine Sorgen, entfernt. Dachte nicht, dass das tatsächlich funktioniert hat. –

+0

danke für die geantwortet. Wenn ich versuche, var hinzuzufügen() und zu entfernen, zeigt die Combobox nichts an. –

+0

@ greybullet171 Mit Combobox meinst du das Dropdown-Menü? –

Verwandte Themen