Welcome Guest, Not a member yet? Register   Sign In
Problems with multiple Jquery checkbox
#1

(This post was last modified: 04-04-2020, 01:03 PM by garciafg.)

Hello everyone! I am trying to send multiple checkboxes to my controller via Jquery, but the data is not going to the controller. before sending the data with an onClick event and it was working perfectly. But with this adaptation to several checkboxes, I'm having difficulties with jquery. Here is the code:


PS: I'm sorry for the mistakes in writing, I translated on google translator.



Controller

Code:
    public function detalhes($id_categoria){
    $categoria=$this->m_home->get_categoria($id_categoria);
    $detalhes_dados = $this->m_home->get_detalhe_produtos($id_categoria);
    $cart_data=$this->Cart->get_cart();
    $added_data= $this->cart->contents();    
    foreach($detalhes_dados as $key => $obj)
    {
      $detalhes_dados[$key]->itens = $this->m_home->get_itens($obj->id_detalhe_produto);
    }
        $this->load->view('detalhes',array('added_data'=>$added_data,'cart_data'=>$cart_data,'detalhes_dados'=>$detalhes_dados,'categoria'=>$categoria));
    }


View

Code:
    <div class="row corpo shadow p-3 mb-5 bg-white rounded">
         <!--FORMULARIO ENVIAR OS DADOS-->
         <forma>             
        <?php
            if(!empty($detalhes_dados)){     
            foreach($detalhes_dados as $row1){
        ?>
        <!-- EXIBE CATEGORIAS E ITENS DAS CATEGORIAS -->
            <div class="panel-group tit-propriedas-cat" id="accordion" role="tablist" aria-multiselectable="true">
                <div class="panel panel-default panel-heading-custom">
                    <div class="panel-heading" role="tab" data-toggle="collapse" data-parent="#accordion" href="#collapse-<?= $row1->id_detalhe_produto ?>" aria-expanded="true" aria-controls="collapse-<?= $row1->id_detalhe_produto ?>" id="headingOne-<?= $row1->id_detalhe_produto ?>">
                        <a role="button">
                            <h4 class="panel-title">            
                            <i class="fas fa-angle-down"></i> <?= $row1->nome_detalhe_produto ?>            
                            </h4>
                        </a>
                    </div>
                    <div id="collapse-<?= $row1->id_detalhe_produto ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne-<?= $row1->id_detalhe_produto ?>">
                    <div class="panel-body">
                        <table class="table table-striped">
                            <?php foreach($row1->itens as $row2): ?>
                                <tbody>
                                    <tr>
                                        <td>
                                            <div class="form-check card-prato product-item" id="product-item-<?php echo $row2->name; ?>">
                                                    <input  type="checkbox"  name="id[]" data-id="<?php echo $row2->id; ?>">
                                                    <label class="form-check-label tit-itens" for="exampleRadios1-<?= $row2->id ?>">
                                                        <b><?php echo $row2->name; ?></b>
                                                    </label>
                                            </div>
                                        </td>
                                        <td>
                                        <span type="badge" class="badge badge-success preco">R$ <?php echo $row2->price; ?></span>
                                        </td>
                                    </tr>
                                </tbody>

                            <?php endforeach; ?>
                        </table>     

                    </div>
                </div>
            </div>
        
        </div>    
        <?php
        }
        } ?>
        <input type="submit" name="enviar" value="Adicionar ao carrinho" class="btn-submit" />
        </forma>
    </div>

Jquery

Code:
<script>
    $(document).ready(function(){
        $('.corpo form').submit(function(){
          $('#mensagemSucesso').empty();
           var id = $(this).find('input[name=id[]').serialize();
            $.ajax({
              type:'POST',
              url:'<?php echo base_url(); ?>home/addtocart',
              dataType:'json',
              data:{
                  'id' : id
               },
              success:function(data){
                $('#mensagemSucesso').append('Dandos inseridos no carrinho').css('visibility','visible').fadeIn('fast').fadeOut(5000);
                  location.reload();
              }
             });
        });
    });
</script>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB