How to mount a dynamic checkbox

2

I have one:

<select name="snh_predio" id="snh_predio"></select>

It loads automatically. When this is changed:

$("#snh_predio").change(function() { });

I want you to print to a table a list of checkbox for selection:

echo "<input type='checkbox' value='" . $sala['snh_sala'] . "' " . $checado . " name='form_snh_sala[" . $sala['snh_sala'] . "]' id='form_snh_sala[" . $sala['snh_sala'] . "]' ";

How can I do this dynamically?

    
asked by anonymous 11.02.2014 / 14:48

3 answers

1

I do not know if I understand, but when you change < select > I guess it will do a search with ajax of the data of the rooms and with this data will have to make a loop printing the checkboxes, is that it?

If this is it, when searching you can generate a json with the key / value pairs and with that loop to generate the html through javascript or even generate the html with php and send this as a string and print on the part you need.

    
11.02.2014 / 15:09
1

You can either use select of type multiple .

<select type="multiple">
    <option value="1">Primeira opção</option>
    <option value="2">Segunda opção</option>
    <option value="3">Terceira opção</option>
    <option value="4">Quarta opção</option>
</select>

But if you really want to use checkbox you will have to build the whole element and all the functionality.

Regardless of how you create it, you should ideally use , so you have separate layers and you can control everything more efficiently and maintainably.

    
11.02.2014 / 15:08
0

With pure Javascript you can do it this way:

form.php

<html>
<head>
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script><scripttype="text/javascript">

        function listarProdutos(categoria){
            $.ajax({
                url: 'listar.php',
                data: {'categoria':categoria},
                success: function(data) {
                    $('#lista').html(data);
                }
            });
        }
    </script>
</head>
<body>

    <form>
        <select id="categoria" name="categoria" onclick="listarProdutos(this.value);">
            <option value="1">JOGOS</option>
            <option value="2">ELETRONICOS</option>
            <option value="3">ROUPAS</option>
        </select>


    Produtos:<br>
    <div id="lista"></div>
    </form>
</body>
</html>

When the combo option is changed it will call the file (example listar.php ), which takes 'input' from the user makes the query in the database (which is replaced by arrays, $ games, $ electronic and $ clothes) creates the checkboxes the HTML output is returned by AJAX and played in div list. This example is simple I recommend that if possible remove echo <input... per template, a suggestion of engine would be Smarty

<?php
function imprimirCheckbox($arr){
    foreach($arr as $item){
        echo '<input type="checkbox"  name="produtos[]" value="'.$item.'" />'. $item .'<br>';
    }
}


$jogos = array('COD', 'FIFA 2014', 'NFS');
$roupas = array('CAMISA', 'CALÇA', 'BERMUDA');
$eletronicos = array('PC','NOTEBOOK','TABLET');

if($_GET['categoria'] == 1){
    imprimirCheckbox($jogos);
}else if($_GET['categoria'] == 2){
    imprimirCheckbox($eletronicos);
}else{
    imprimirCheckbox($roupas);
}
    
11.02.2014 / 16:13