How to pass a selected value in a datatable through POST?

1

I am using a DataTable to display information and I have a select with another group of data

I would like to be able to give it to the $_POST fix the value that I select in the table and the select one. This last one I have, but I do not know how I can add the value of the row at the moment of executing the POST with the button

I capture the data with the following JS

    $('#tClientesEmb tbody').on('click','tr', function(){
        var data = table3.row($(this).closest('tr')).data();
        //console.log(data[0]);
    });

How can I insert that data[0] to use it in the controller?

So I have the code in sight

    <?php echo form_open('Config/AsocCliEmb'); ?>
        <div class="form-group" >
            <div class="row">
                <div class="col-sm-9" id="cAsocEmb">
                <table id="tClientesEmb" class="display" cellspacing="0" width="100%">
                    <thead>
                        <tr class="tClientesEmb" data-id="id">
                            <th>N Embarque</th>
                            <th>Fecha Emb.</th>
                            <th>Destinatario</th>
                            <th>Referencia</th>
                            <th>Cajas</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
                </div>
                <div class="col-sm-3">
                    <label for="Cliente" class="control-label col-sm-3">Cliente</label>
                    <select id="cliente" name="cliente" class="selectpicker">
                        <?php foreach ($clientes as $c) echo '<option value="'.$c->id.'">',$c->nombre,'</option>'; ?>
                    </select>
                    <div class="form-group"> <!-- Submit Button -->
                        <div class="col-sm-9 col-sm-offset-2">                     
                            <button type="submit" class="btn btn-primary">Guardar</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php echo form_close();?>
    
asked by Gonzalo Oyarce 05.06.2017 в 22:53
source

3 answers

0

After finding out, comparing answers and using different techniques, I achieved my goal in the following way

    $('#tClientesEmb tbody').on('click','tr', function(){
        var data = table3.row($(this).closest('tr')).data();
        //console.log(data[0]);
        $(this).append(
            $('<input />')
                .attr('type','hidden')
                .attr('name','n_embarque')
                .val(data[0])
            );
    });

Use append to add the value of the type hidden variable

    
answered by 08.06.2017 / 18:32
source
0

using ajax

 $('#tClientesEmb tbody').on('click','tr', function(){
            var tr = table3.row($(this).closest('tr')).data();
            $.ajax({
                method: "POST",
                url: "tu_url",
                data: { id: tr}
            })
            .done(function( msg ) {
                //algo que haces con la respuesta
            });
        });

without using ajax

 $('#tClientesEmb tbody').on('click','tr', function(){
     var tr = table3.row($(this).closest('tr')).data();
     $('<form action="tuurl.php" method="POST">' + 
       '<input type="hidden" name="id" value="' + tr+ '">' +
       '</form>').submit();
});
    
answered by 06.06.2017 в 02:20
0

If I understood what you want is to make dependent selects, right? If this is the case, using ajax, you should do this

in header

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
   $("#categoria").change(function () {
       $("#categoria option:selected").each(function () {
            micategoria=$('#categoria').val();
            $.post("<?= base_url('index.php/selects_con_ajax_llenacombo')?>", 
                { micategoria: micategoria}, 
                function(data){
                    $("#subcategoria").html(data);
                }
            );            
       });
    })
});
</script>

in the body

   <select name="categoria" id="categoria">
        <option value="">Selecciona una categoría</option>
        <option value="1">Empleo</option>
        <option value="2">Contactos</option>
        <option value="3">Antigüedades</option>
    </select>
     <label>Selecciona una subcategoría</label>
    <select name="subcategoria" id="subcategoria">
        <option value="">Selecciona una subcategoría</option>
    </select>

in application / config / routes.php

$route['selects_con_ajax_llenacombo'] = 'selects_con_ajax/micombo/llenacombo';

in application / controllers / selects_con_ajax / micombo.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Selects_con_ajax extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->model('model_busca');
    }

    public function micombo(){
        if( ! $this->input->is_ajax_request()) {
            exit('No direct script access allowed');
        }
        else{
            $categoria = $this->input->post('micategoria', TRUE);
            $sub_category = $this->model_busca->get_subcategory_by_id($categoria);

            $data['subcategory'] = $sub_category;

            print($this->load->view('selects/subcategory', $data, TRUE));
        }
    }
}

in application / views / selects / subcategory.php

<?php foreach($subcategory as $item => $value):?>
    <option value="<?= $item?>"><?= $value?></option>
<?php endforeach;?>

in application / models / model_busca.php

class Model_busca extends CI_Model {
    function get_subcategory_by_id($id_categoy){
        return $this->db->get_where('sub_categories', array('fk_category' => $id_category))->result();
    }
}
    
answered by 06.06.2017 в 16:39