Pass checkbox list to php

2

How can I pass a php page to a selected checkbox list, I have this that shows me the checkboxes, but I'm having trouble sending them to php.

    function SelecionaChecks() {
    var Check = document.getElementsByName("check");                
    for (var i=0;i<Check.length;i++){  
        if (Check[i].checked == true){  
            // CheckBox Marcado
            alert(Check[i].value + " selecionado.");

        }  else {
            // Nenhum checkbox marcado
        }
    }
}

My checks are coming from this mark:

        <div class='row'>
        <div class='span12'>
            <div class='barra-btn table-bordered'>
                <div class='btn-group'>
                    <button class='btn btn-large' type='button' title='Marcar/Desmarcar todos' id='todos' onclick='marcardesmarcar();'><i class='icon-large  icon-ok'></i></button>
                    <button class='btn btn-large' type='button' title='Imprimir'><i class='icon-large icon-print'></i></button>
                    <button class='btn btn-large' type='button' title='Enviar mensagem' onclick="SelecionaChecks()" ><i class='icon-large icon-message-plus'></i></button>
                    <button class='btn btn-large' type='button' title='Adicionar coment&aacute;rio'><i class='icon-large icon-notes'></i></button>
                    <button class='btn btn-large' type='button' title='Relacionar com uma vaga'><i class='icon-large icon-inbox-plus'></i></button>
                </div>
            </div>
        </div>
    </div>

From this loop in this form:

        <div class='row'>
        <div class='span12'>
            <form id="selecao">
            <table class='table table-bordered'>
                <tbody>
                <?php
                    do { 
                    if ($totalRows_rsRegistro > 0) { 
                    // Resgatando ID´s para gerar pdf e envio de e-mails
                    $IdCandidato = $row_rsRegistro['id_candidato'];

                ?>
                    <tr>
                        <td width='2%'><input type='checkbox' class='marcar' name='check' id='check' value="<?php echo $IdCandidato; ?>"/></td>
                        <td width='5%' align='center'>
                            <div class='btn-group btn-group-vertical'>
                                <button class='btn btn-small' type='button' title='Imprimir'><i class='icon-large icon-print'></i></button>
                                <button class='btn btn-small' type='button' title='Enviar mensagem'><i class='icon-large icon-message-plus'></i></button>
                                <button class='btn btn-small' type='button' title='Adicionar coment&aacute;rio'><i class='icon-large icon-notes'></i></button>
                                <button class='btn btn-small' type='button' title='Relacionar com uma vaga'><i class='icon-large icon-inbox-plus'></i></button>
                            </div>
                        </td>
                        <td width='70%'><span class='titulo_18 bold'><a href="admin/vercurriculo.php?id=<?php echo $IdCandidato; ?>" class="a-titulo"><?php echo $row_rsRegistro['nome']; ?></a></span></br>
                        <?php echo $row_rsRegistro['email']; ?></br>
                        <?php echo $row_rsRegistro['celular']; ?></br>
                        <?php echo $row_rsRegistro['id_municipio']; ?> | <?php echo $row_rsRegistro['id_uf']; ?></td>
                        <td width='23%'><span class='titulo_14'>Dados gerais</span>
                        </td>
                    </tr>
                </tbody>
                <?php
                } else {
                    echo "<div align='center'>N&atilde;o existe(m) curriculo(s) para ser(em) exibido(s)</div></br>";
                    }
                } while ($row_rsRegistro = mysql_fetch_assoc($rsRegistro)); 
                ?>
            </table>
            </form>
        </div>
    </div>
</div>
    
asked by anonymous 02.06.2014 / 22:38

3 answers

7

It is unclear how you trigger the process, ie what button you load to send data to the php side (server side). I leave an example below. What you need is ajax, to pass data to and from the server side.

An example function, called, ajax is:

$.ajax({
  url: "ficheiroPHP.php", // url do ficheiro php
  type: "POST",           // tipo de método POST, GET, etc
  data: { id : menuId },  // dados a enviar, objeto.
  success: function(retorno){
     // aqui a variável "retorno" contém a resposta do ficheiro php
  }
});

The data field is where you can pass data to the server. Here you can do it in different ways but the most practical is to use .serialize (), for example:

var dados = $("form").serialize();

and then inside ajax:

data: dados,  // dados a enviar, objeto.
    
03.06.2014 / 07:31
2

Only the checked items are sent to php, to get all it is necessary to add brackets [] in the checkbox name otherwise the last value is sent.

change:

 <input type='checkbox' class='marcar' name='check' id='check'
  value="<?php echo $IdCandidato; ?>" />

To:

<input type='checkbox' class='marcar' name='check[]' id='check'
 value="<?php echo $IdCandidato; ?>" />

To list them just do a forech:

if(count($_POST['check']) > 0){
   foreach($_POST['check'] as $item){
       echo $item .'<br>';
   }
}
    
03.06.2014 / 12:52
1

Follows how my code was in line with the suggestions received.

    function SelecionaChecks() {
var checked = []; 
$("input[name='check[]']:checked").each(function () 
{
    checked.push(parseInt($(this).val()));
});

    // console.log(checked);    
    $.ajax( {
        url:'enviaEmail.php',
        type:'POST',
        data: {list:checked},
        success: function(res) {
            alert(res);
        }
    });     

}

I redeem the checked checks, set an array, and step into my php script. Thanks to @Sergio and @ Lost for the great help.

    
03.06.2014 / 21:23