-1

Olá!

Estou montando um recurso onde preciso enviar multiplos registros escolhidos em uma grade clicando em seu respectivo checkbox... alguém sabe como guardar esses id's escolhidos e enviar via ajax/jquery para um arquivo php que irá realizar um outro processo?

código:

html:

<select class="btn_dGrid_btnActionWithSelecteds" name="btn_dGrid_btnActionWithSelecteds"> <option value="">A&ccedil;&atilde;o com os Selecionados</option> <option value="opt1">Opção1</option> </select>

jquery:

    $(".btn_dGrid_btnActionWithSelecteds").change(function(){
     var id_escolhido = $(".dynamicGridCheck").val();
   $.ajax({
                    url: "engine/php/gerar.php?&&id_escolhido="+id_escolhido,
success: function( data ){
   alert(data);
}
});

grade:

<tr>
   <td><input type="checkbox" value='$id'></td>
   <td>Registro1</td>
</tr>
<tr>
   <td><input type="checkbox" value='$id'></td>
   <td>Registro2</td>
</tr>

gerar.php:

 $id_escolhido []= $_REQUEST['id_escolhido'];
foreach($id_escolhido as $ids){
        echo "id: ".$ids;
        echo "\n";
    }

...Atualmente apenas um registro aparece no php, msmo eu selecionando varios...

1 Resposta 1

0

Resolvi pessoal, segue o código completo:

<meta charset="utf-8">

<style>
    table{
        display:block;
        border:1px solid #ccc;
    }
    table thead{
        display:block;
        border:1px solid #ccc;
    }
    table tbody{
        display:block;
    }
    table tbody tr{
        display:block;
        border:1px solid #ccc;
    }
    table td:first-child,
    table th:first-child{       
        min-width:50px;
    }
    table td,
    table th{
        text-align:left;
        min-width:100px;
    }   
</style>

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>

<form id="form1">

    <select name="btnGerar" class="btnGerar">
        <option value="">Ação com os selecionados</option>
        <option value="gerar">Gerar</option>
    </select>

    <br>
    <br>

    <table>
        <thead>
            <tr>
                <th></th>
                <th>Nome</th>
                <th>Endereço</th>
            </tr>
        </thead>

        <tbody>
            <tr>
                <td><input type="checkbox" name="gridCheck[]" value="1" class="gridCheck"></td>
                <td>John</td>
                <td>Rua Abc, 451</td>
            </tr>

            <tr>
                <td><input type="checkbox" name="gridCheck[]" value="2" class="gridCheck"></td>
                <td>Megg</td>
                <td>Rua xxx, 123</td>
            </tr>
        </tbody>
    </table>
</form>

<script>
$(document).ready(function(){

    $(".btnGerar").change(function(){

        var btnGerar = $(".btnGerar").val();    
        var gridCheck = $(".gridCheck").serialize();    

        if(btnGerar=="gerar"){                      

              var urlSet = "process/gridCheckProcess.php?gridCheck[]"+gridCheck;

              $.ajax({                          
                  url: urlSet,                
                  success: function(gridCheck){
                    alert(gridCheck);
                  },
                  error: function(){
                    alert('failure');
                  }
                });

        }else{
            //...   
        }//end if btnGerar

    });//end change 

});//end do
</script>

<?php

    $gridCheck = $_REQUEST['gridCheck'];
    //var_dump($gridCheck);

    foreach($gridCheck as $id){
        echo "id: ".$id;
        echo "\n";
    }   

?>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .