Submit form with empty checkboxes and receive their value on the server

Asked

Viewed 683 times

0

I’m picking up a form with AJAX and Jquery and sending for treatment in a file PHP by name getPDF, in this form there are several checkboxes with different values however name and class equal (name='check[]' and class='toggle-check'). I know how to get the values in the checkbox fields checked with foreach, however there is some way that I get all values of all checkbox form independent of whether they are checked or not ?

PHP code (Where only checkbox checked are handled):

   <?php
    include_once "../Data/config.php";
    foreach($_POST['check'] as $check) {
       $result="SELECT * FROM indicador WHERE nome = '".$check."'";
       $resultado = mysqli_query($dbc,$result);
         while($row_indicador = mysqli_fetch_assoc($resultado)) {
          echo'teste com sucesso ';
         }
     }

Javascript code that sends the values to the PHP

  jQuery(document).ready(function(){
 //envio o formulário para tratamento no getPDF.php
        jQuery('#formVerIndicadores').submit(function(){
            var dados = jQuery( this ).serialize();

            jQuery.ajax({
                type: "POST",
                url: "getPDF.php",
                data: dados,
                success: function(data)
                {


                    $('#resultadoPesquisaIndicador').html(data);
                }
            });

            return false;
        });
    });

Html code that was generated by PHP :

      <form id='formVerIndicadores' method='post'>
      <tr>
       <td>Carne bovina de corte</td>
        <td>
         <div><input class="toggle-check 1" name="check[]" id="" value="Carne   bovina  de corte" type="checkbox"><span></span></div>
        </td>
      </tr>
       <tr>
       <td>Bovino (kg*Pa)</td>
        <td>
        <div><input class="toggle-check 2" name="check[]" id="" value="outro valor qualquer" type="checkbox"><span></span></div>
        </td>
       </tr>
  //Pode ter N checkboxes dependendo da seleção em outro arquivo php
 <input type='button' value='Enviar'/>
      </form>
  • is the same thing, the point is that if it’s not marked it comes as false

  • And how do I get it back in my php ?

  • how are checkboxes mounted on the form? What is the form code like? You can post the form page code?

  • Added, the only thing that changes from one checkbox to another is the other class that increases by 1 in 1

  • Adding manually? Or with a while php?

  • While browsing the database, you want me to put the part that generates the checkboxes ?

  • Only the while line is good, can be in comment while .....{

Show 2 more comments

1 answer

2


On the page Código Html que foi gerado pelo PHP:

Comments on the code

while ($rows = ........
    .........
    .........
    //concatenando todos os valores do checkboxes
    $all_checkbooxes .=$rows['nomeColuna'].","; 
}
//fim while
//input type hidden com todos os valores dos checkboxes
// no value uma função para retirar a ultima virgula
echo "<input type='hidden' name='todos' value='".substr($all_checkbooxes, 0, -1)."'>";

PHP Code (Where all checkboxes are handled):

include_once "../Data/config.php";
foreach($_POST['check'] as $check) {
   $result="SELECT * FROM indicador WHERE nome = '".$check."'";
   $resultado = mysqli_query($dbc,$result);
     while($row_indicador = mysqli_fetch_assoc($resultado)) {
      echo'teste com sucesso ';
     }
}

$checados = $_POST['check'];

$todos = $_POST['todos'];

$explode = explode(",",$todos);
//retorna os valores que não estão presentes no array dos checados
$naoChecados=array_diff($explode,$checados);

foreach($naoChecados as $noCheck) {
    //o código para usar com os checkboxes que não foram checados
}

Javascript code that sends values to PHP: nothing to change

OBS

Use um `<button type='submit'`

<button type='submit' value='Enviar'/>Enviar</button>
<div id="resultadoPesquisaIndicador"></div>
</form>

About the type attribute of Buttons

  1. Submit: The button sends the form data to the server.
  2. button: The button has no default behavior. It may have client-side scripts associated with element events, in which they are triggered when the event occurs.

<button> - Mozilla Developer Network

  • Thank you my dear, at the time had not thought to create some Hidden checkboxes even

Browser other questions tagged

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