gmc1103 Posted February 11, 2016 Share Posted February 11, 2016 Hi I have a select dropdown where the user select an item and the second dropdown shows the options in multiple select options. From my database i have $val = filter_input(INPUT_POST, 'get_option'); if($val!=null){ try { $sql = "SELECT t1.`user_proc`, t1.`user_name` FROM `esmaior_alunos` as t1 INNER JOIN `esmaior_turma` as t2 ON (t1.`user_id_ano_turma` = t2.`id_turma`) WHERE (t2.`id_turma` = :val);"; $query = $DB_con->prepare($sql); $query->bindparam(":val", $val); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); echo json_encode($result); } catch (PDOException $e) { echo $e->getMessage(); } } And i get the result here <script> $(document).on("change", '#turma', function(e) { var turmas = $(this).val(); $.ajax({ type: 'post', url: 'classes/getFromDatabase.php', data: {get_option:turmas}, dataType: 'json', success: function(resposta) { var $el = $("#alunos"); $el.empty(); var nomes = resposta.map( function( el ){ var processo = []; processo.push(el.user_proc); for (var i = 0; i < processo.length; i++) { getNumerosProcessos(processo); } return el.user_name; }); $.each(nomes, function(value, key) { $el.append($("<option></option>").attr("value", value).text(key)); }); } }); }); </script> This is working but the main problem it's i have to get the "user_proc" into my multiple select option value but i can't have that since this is dynamic My select is this <div class="form-group"> <label for="exampleInputFile">Alunos Ausentes (tecla "shift" para seguidos, "ctrl" para separados)</label> <select name="alunos[]" id="alunos[]" class="form-control" multiple title="Escolha os alunos" style="height: 240pt;"> <option value=""></option> </select> </div> So with this code how i can pass to <option value "user_proc">Name> any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/300794-help-with-dynamic-multiple-select/ Share on other sites More sharing options...
requinix Posted February 12, 2016 Share Posted February 12, 2016 id="alunos[]"That's wrong. The ID should just be "alunos". The [] is only for the name. Quote Link to comment https://forums.phpfreaks.com/topic/300794-help-with-dynamic-multiple-select/#findComment-1531036 Share on other sites More sharing options...
gmc1103 Posted February 12, 2016 Author Share Posted February 12, 2016 Thank you for helping me I have removed that but i still have the same problem, i receive this in my php Array ( [data] => 2016-02-12 [hora] => 12:12 [ati] => fertertert [turma] => 17 [alunos] => Array ( [0] => 1 [1] => 4 [2] => 5 ) [docente] => 88 ) And [alunos] don't have the user_proc number wich is by that order 15497 15789 15805 and i receive 1, 4 and 5 So i need pass to <option value "user_proc">user_name> to get the correct values in my database Quote Link to comment https://forums.phpfreaks.com/topic/300794-help-with-dynamic-multiple-select/#findComment-1531046 Share on other sites More sharing options...
Solution gmc1103 Posted February 12, 2016 Author Solution Share Posted February 12, 2016 I have fixed using if($val!=null){ try { $sql = "SELECT t1.`user_proc`, t1.`user_name` FROM `esmaior_alunos` as t1 INNER JOIN `esmaior_turma` as t2 ON (t1.`user_id_ano_turma` = t2.`id_turma`) WHERE (t2.`id_turma` = :val);"; $query = $DB_con->prepare($sql); $query->bindparam(":val", $val); $query->execute(); $result = $query->fetchAll(PDO::FETCH_ASSOC); foreach ($result as $row) { echo "<option value='{$row['user_proc']}'>{$row['user_name']}</option>\n"; } } catch (PDOException $e) { echo $e->getMessage(); } } and the ajax <script> $(document).on("change", '#turma', function(e) { var turmas = $(this).val(); $.ajax({ type: 'post', url: 'classes/getFromDatabase.php', data: {get_option:turmas}, dataType: 'html', success: function(resposta) { console.log(resposta); document.getElementById("alunos").innerHTML=resposta; } }); }); </script> For those with same problem Quote Link to comment https://forums.phpfreaks.com/topic/300794-help-with-dynamic-multiple-select/#findComment-1531050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.