2

I have a form where the fields come from the database. Look:

public function tamanhosTabelas($key)
{
  while($isfast = mysqli_fetch_object($sql))
  {
  ....
  $checked = $isfastTamanhos->IdTamanhos == $isfast->IdTamanhos ? 'checked' : null;
            $visualizar .= '<div class="bloco Grade"><input type="checkbox" name="TamanhoGrade[]" 
   value="'.$isfast->IdTamanhos.'" '.$checked.'><br><span style="font-weight: bold">'.$isfast- 
   >Tamanhos.'</span></div>';
  }
return $visualizar;
}

I have the following result:

Image

However, I need to check which field was selected or not. My code looks like this:

if($_POST)
{
    ....
    $tamanhoGrade = $_POST["TamanhoGrade"];
    echo $metodos->alterarGrade($tamanhoGrade);
}
    

Method alterarGrade($tamanhoGrade):

    public function alterarGrade($tamanhoGrade)
    {
      for($i = 0; $i <= count($tamanhoGrade); $i++)
      {
        if($tamanhoGrade[$i] != null)
        {
          echo "Selected sizes " .$tamanhoGrade[$i]."<br>";
        }
          else
        {
          echo "Unselected sizes " .$tamanhoGrade[$i]."<br>";
        }
      }
    }

But it only brings the selected fields. I would like to take the fields that are not marked to exclude from the database. I've tried using the isset(), but I also couldn't.

Sorry for my English.

JMarcos
  • 23
  • 3

2 Answers2

0

If you wanna see wether a checkbox is checked or not Check this out : How to read if a checkbox is checked in PHP?

if (isset($_POST['test'])) {
//the test checkbox is checked
} else {
//the text checkbox is not checked
}
Chris
  • 1
  • 2
0

The solution I found was:

I took the field values name="TamanhoGrade[]"and put it in the implode taking only the values ​​marked:

public function alterarGrade($tamanhoGrade)
{
 .....
$tamanhos = implode(",",$tamanhoGrade); 

Having the fields checked, I deleted it directly in the database with NOT IN:

"DELETE FROM table WHERE IdTamanhos NOT IN (".$tamanhos.")"
JMarcos
  • 23
  • 3