Retrieve dynamic checkbox value in php

Asked

Viewed 611 times

0

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

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 I do this? Should I put my table inside a form and send the form to another page? I actually tried to do this, so I put this table in a form, and clicking the button sends the data to a Delete.php page

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

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

1 answer

1

Summing up by what I understand you want to get the checkbox value 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;

}

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

print_r($_POST);
// e depois podes tratar as checkboxes contidas em $_POST
// ex: $checkboxes = $_POST['checkbox']
  • That was my question, thank you very much for your reply. but with respect to loop repeating to get the value of all my checkbox, as it is possible to do?

  • gives up the answer, you can use the function each of jquery example: $('.classe').each(function(indice, obj) {&#xA; &#xA;});

  • I’ll give up, but I keep facing problems, I did an if to know if I was getting the value in php page but I’m not, how is it possible to see the values of the serialized data? it is possible to send only the checkbox field ?

  • 1

    It was my mistake, I was able to identify the problem. Thank you for the solution

Browser other questions tagged

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