how to obtain values in a MULTISELECT using JSON?

1

I have a dni field and a multiselect technology, in technology I added a function to show it with check boxes () which I want to show through JSON, but I do not drag anything, I was thinking about adding an ajax inside the function , but I would not know how to do it

<body>
  <center>
    <br/>
  <div id="contenedor">
    <br/><br/>
    <label>DNI</label>
    <input type="text" id="dni"> 

    <select  id="tecnologia" name="tecnologia[]" multiple="multiple" size="5">
     <option value="2g">2G</option> 
      <option value="3g">3G</option> 
     <option value="4g">4G</option> 
     <option value="tdd">TDD</option>
      </select>

     <script type="text/javascript">
    $(function() {
        $('#tecnologia').multiselect({
        buttonText: function(options, select){
          console.log(select[0].length);
          if (options.length===0) {
            return 'sin Seleccion';
          }
          if (options.length===select[0].length) {
            return 'todos ('+select[0].length+')';
          }
          else if(options.length>=4){
            return options.length+'seleccionados';
          }else{

            var labels=[];
            console.log(options);
            options.each(function(){
              labels.push($(this).val());
            });
            return labels.join(', ')+'';
          }

        }
      });
    });
</script>
</br>

  </br></br>
    <button onclick="anadir();">Añadir</button>
    <input type="submit" onclick="buscar()" value="buscar">
  </div>

</body>
</html>

at this point .js performed the ajax

function buscar(){

    var parametros={"dni": $("#dni").val(),}

    $.ajax({

            type: 'post',
            url: '../lunes-2/buscardatos.php',
            data: parametros,
            dataType: 'json',
            success: function(d){

                $.each(d[0].split(","), function(i,e){
                    $("#tecnologia option[value='"+e+"']").prop("selected",true);
                });

            }
        });
}

y aca arrastro los valores de mi base de datos mediante JSON

<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("archivos",$con);

$dni=$_POST["dni"];
$rs=mysql_query("SELECT * FROM clie  WHERE dni='$dni'");

$row = mysql_fetch_assoc($rs);

$datos_a_enviar = array($row['tecnologia']);
echo json_encode($datos_a_enviar);
?>

What I try is to show me the values of my database in the MULTIPLE select, but since I am adding a function in the body (which works in the best way) I would not know how to modify the code there so that when I enter my dni and click on search to show me the options that were selected and stored in my BD

    
asked by kev 26.04.2018 в 19:14
source

1 answer

0

You have to refresh the multiselect. Try this way:

$.ajax({

        type: 'post',
        url: '../lunes-2/buscardatos.php',
        data: parametros,
        dataType: 'json',
        success: function(d){

            $.each(d[0].split(","), function(i,e){
                $("#tecnologia option[value='"+e+"']").prop("selected",true);
            });
            $("#technologia").multiselect("refresh");
        }
    });
    
answered by 26.04.2018 / 19:27
source