I wanted to know how to use two select multiple tags to insert data into the database?

0

JS:

function insertSelected(campoOrig, campoDest){
   x = campoOrig.value;
   if (x == "") {
      alert("Selecione um Item"); 
      return;
   }
   Origem = campoOrig;
   Destino = campoDest;
   var len = Destino.length;
   for (var i = 0; i < Origem.length; i++) {
      if ((Origem.options[i] != null) && (Origem.options[i].selected)) {
         Destino.options[len] = new Option(Origem.options[i].text, Origem.options[i].value);
         len++;
         Origem.options[i] = null;
         i--;
      }
   }
}
$(function($){
   $("#disciplinas").on('change',function(){
      iddisc = document.getElementById("disciplinas").value;
      $("#Enviar").click(function(){
         $.post("cadastro.php",{discid:iddisc,acao:"CadastrarDiscTur"},function(retorno){
            $(".error").html(retorno);
         });
      });
   });
   $("#turma-disciplina").on('change',function(){
      eiddisc = document.getElementById("turma-disciplina").value;
      $("#Excluir").click(function(){
         $.post("cadastro.php",{discid:eiddisc,acao:"ExcluirDiscTur"},function(retorno){
            $(".error").html(retorno);
         });
      });
   });
});

HTML:

<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<table>
   <tr>
      <td>
         <select id="disciplinas" name="disc" multiple><!-- Caixa de seleção para as disciplinas -->
            <?php foreach($discs as $disc){ ?>
            <option value='<?=$disc['disc_id']?>'><?=$disc['disc_nome']?></option><!-- Loop para mostrar todas as disciplinas cadastradas -->
            <?php } ?>
         </select>
      </td>
      <td>
         <input type="button" id="Enviar" value=">" onclick="inserir(); insertSelected(getElementById('disciplinas'),getElementById('turma-disciplina'));">
         </p>
         <input type="button" id="Excluir" value="<" onclick="insertSelected(getElementById('turma-disciplina'),getElementById('disciplinas'));" >
      </td>
      <td>
         <select id="turma-disciplina" multiple>
            <?php
            if($turDisc != NULL){ 
               foreach($turDisc as $Tdisc){ ?>
            <option value='<?=$Tdisc['disc_id']?>'><?=$Tdisc['disc_nome']?></option>
            <?php } 
            } ?>
         </select>
      </td>
   </tr>
</table>
<div class="error"></div>

PHP:

if ($_POST['acao'] == "CadastrarDiscTur") {
        insertDiscByTur($_SESSION['idturma'],$_POST['discid']);
        unset($_POST['discid']);
        unset($_POST['acao']);
    }else if ($_POST['acao'] == "ExcluirDiscTur") {
        deleteDiscTurById($_SESSION['idturma'],$_POST['discid']);
        unset($_POST['discid']);
        unset($_POST['acao']);
    }
function insertDiscByTur($turma,$disc){
    $link = openDb();
    $sql = "INSERT INTO turma_disciplina(tur_id,pro_id,disc_id) VALUES('$turma',NULL,'$disc')";
    echo $sql;
    $link->query($sql);
    $link->close();
}

function deleteDiscTurById($turma,$disc){
    $link = openDb();
    $sql = "DELETE FROM turma_disciplina WHERE tur_id = '$turma' AND disc_id = '$disc'";
    echo $sql;
    $link->query($sql);
    $link->close();
}
    
asked by anonymous 14.12.2017 / 18:38

0 answers