How to return values with JSON in a multiselect


I have the following index, which includes a multiselect (fselect.css and fselect.js) which is shown in the following image:

and my code is as follows:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Registro Mantenimiento</title>
<meta name="viewport" content="initial-scale = 1.0, maximum-scale = 1.0, user-scalable = no, width = device-width">

<link rel="stylesheet" href="style.css" media="screen">
<link rel="stylesheet" href="style.responsive.css" media="all">
<link rel="stylesheet" href="" />  

<script src=""></script>
<link href="multiselect/fSelect.css" rel="stylesheet">
<script src="multiselect/fSelect.js"></script>
  (function($) {
      $(function() {
<link rel="stylesheet" type="text/css" href=";subset=latin">

<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" />
   <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/funciones.js"></script>
<div id="contenedor" style="width: 300px">
<input type="text" id="dni"> 

<input type="text" id="nombre"> 

<input type="text" id="apellido"> 


 <select class="test_3" id="tecnologia" name="tecnologia[]" multiple="multiple" style="height:10px;">
    <option value="Algebra">Algebra</option> 
    <option value="Aritmetica">Aritmetica</option> 
    <option value="Lenguaje">Lenguaje</option> 
    <option value="Literatura">Literatura</option>
    <option value="Historia Universal">Historia Universal</option> 
    <option value="Biologia">Biologia</option>
    <option value="Quimica">Quimica</option>
    <option value="Fisica">Fisica</option>

 <button onclick="anadir();">Añadir</button>
 <button onclick="actualizar();">Modificar</button>
 <button onclick="eliminar();">Eliminar</button>
 <input type="submit" onclick="buscar();" value="buscar">

 <div id="ventana"> 


Then when I save my multiselect, the selected values are saved in the database "separated by commas". and the moment I use my jSON to bring values I call them in the following way with the search function ():

function buscar(){

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


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

            $.each(d[2].split(","), function(i,e){


and my buscadatos.php is the following

 $con = mysql_connect("localhost","root","");

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

  $row = mysql_fetch_assoc($rs);

  $datos_a_enviar = array($row['nombre'], 
   echo json_encode($datos_a_enviar);

then only returns the values of first and last name, but not of the multiselect, as seen in this image:

How would you do to select the values that you mark when you save the record, if I am storing them delimited by "commas"?

asked by Kevincs7 21.11.2018 в 20:36

1 answer


What Bryro tells you is correct but you do not make any assignments, it should be something like this:

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

In the loop each is where you should put the checked to the corresponding options.

Personally I would use numeric indexes, make operations with literal text is not flexible (capitals and spaces are a headache, misspellings, languages ....) and the resulting JSON is smaller.

Something like:

 <select class="test_3" id="tecnologia" name="tecnologia[]" multiple="multiple" style="height:10px;">
    <option id="tec_1" value="1">Algebra</option> 
    <option id="tec_2" value="2">Aritmetica</option> 
    <option id="tec_3" value="3">Lenguaje</option> 
    <option id="tec_4" value="4">Literatura</option>
    <option id="tec_5" value="5">Historia Universal</option> 
    <option id="tec_6" value="6">Biologia</option>
    <option id="tec_7" value="7">Quimica</option>
    <option id="tec_8" value="8">Fisica</option>

and the update loop would be:

        $.each(d[2].split(","), function(i,e){
            $('#tec_'+e).prop('checked', true);

Additional information about the check: link

answered by 22.11.2018 в 01:20