Retrieve dynamic checkbox value in php

0

I have a table where one of your columns is a checkbox, I need to do when the checkboxes are selected click on a "Delete Selected" button where it is possible to retrieve for me all the selected Checkbox values.

This HTML table comes from an Ajax request, where it is mounted in HTML, so it is dynamic.

while ($linha = $result->fetch_assoc()) 
{
  $codigo= utf8_encode($linha["codigo"])." "
  $return.= "<td>". "<input type=\"checkbox\" name=\"cb\"  value=\"".$codigo."\"  >  "."</td>";

  $return.= "<td>" . utf8_encode($linha["data"]) . "</td>";
  $return.= "<td>" . utf8_encode($linha["hora"]) . "</td>";
  $return.= "<td>" . utf8_encode($linha["origem"]) . "</td>";
  $return.= "<td>" . utf8_encode($linha["destino"]) . "</td>";

}

How can you do this? should I put my table inside one form and send the form to another page? Currently I tried to do this, so I put this table in a form, and clicking the button sends the data to an Exclude.php page

I tried to use the following command to retrieve the value foreach ($ _POST as $ key = > $ value) {     echo $ key; // Here was the code value, but only the name of a button appears, leaving "cb" }

So, how do I retrieve the code? is there a better way to do this? I would also like to know if it is possible to send all selected values via Ajax to the server, how can I resolve this?

    
asked by anonymous 28.07.2017 / 21:04

1 answer

1

In short, what do you want to get the value of the checkbox that the user chose?

I'll give you an example:

HTML

<form id="form" class="form" method="post" name="meuform">
<textarea id="texto" type="text" name="texto"></textarea>
<input type="checkbox" name="checkbox[]" id="checkbox" value="valor1" />
<input type="checkbox" name="checkbox[]" id="checkbox" value="valor2" />
<input id="submit" type="submit" name="submit" value="Submit" onclick="return submitForm()" />
</form>
<div id="resposta"></div>

Jquery

function submitForm() {
var form = document.myform;

// obter todos os campos do form
var dataform = $(form).serialize();

// fazer um post ao arquivo.php (como exemplo)
$.ajax({
    type:'POST',
    url:'arquivo.php',
    data: dataform,
    success: function(data){
       // resposta que obtiveste (so para confirmar que esta tudo ok. )
        $('#resposta').html(data);

    }
});

return false;

}

NO .php file you can give a simple print_r in the POST global variable ex:

print_r($_POST);
// e depois podes tratar as checkboxes contidas em $_POST
// ex: $checkboxes = $_POST['checkbox']
    
28.07.2017 / 21:22