Pass checkbox list to php

Asked

Viewed 2,000 times

2

As I can pass to a php page a checkbox list selected, I have this that shows me the checkbox, 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 that appointment:

        <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 that loop on that 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>
  • Can’t use <form>?

  • I don’t understand your question Sergio.

  • 1

    Good you have edited the question and added code! Now I see you already have one <form> but without "action". You want to send and change/reload the page or send keeping the page?

  • Keeping the Sergio page, later I understood your question and so I decided to edit the post.

3 answers

7


It is unclear how you trigger the process, ie which button you press 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 of function, call, 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
  }
});

On the field data 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 the ajax:

data: dados,  // dados a enviar, objeto.

2

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

change:

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

for:

<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>';
   }
}

1

Follow how my code was according to 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);
        }
    });     

}

Rescues the checked checks, assembles an array and passes to my php script. Thanks to @Sergio and @lost for the great help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.