Jump to content

Help with dynamic multiple select


gmc1103
 Share

Go to solution Solved by gmc1103,

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.