how to get the checked checkbox with php from a table with sql ← (PHP, MySQL, CSS, HTML)

the code is:

    function formulariconsultes($resultat){
        echo "<form name='comanda' method='post' action='../controlador/controladorcomanda.php'>";
        echo "<table style='border: solid 1px black;'>";
        echo "<td><b>codi</b></td>
        <td><b>nom</b></td>
        <td><b>descripcio</b></td>
        <td><b>pes</b></td>
        <td><b>stock</b></td>
        <td><b>categoria</b></td>
        <td><b>Marca</b></td>
        <td><b>Cantitat</b></td>";
        while ($row = mysqli_fetch_array($resultat)){
        echo "<tr><td>". $row['codProd'] .
         "</td><td>" . $row['nom']. 
         "</td><td>". $row['descripcio']. 
         "</td><td>". $row['pes']. 
         "</td><td>". $row['stock']. 
         "</td><td>". $row['idCategoria'].
         "</td><td>". 
         "<input type='checkbox' name='marcar[]' value='hola'>".
         "</td><td>".
         "<input type='number' name='cantitat[]' >".
         "</td></tr>";
        }
        echo "</table>";
        echo "<input type='submit' name='enviarcomanda' style='margin-top:10px; margin-left:60px; height:60px; width:120px;font-size: 25px;'>";
        echo "</form>";
    }

with this function i need to get every checkbox that is checked in order to see the rows that i selected to make a confirm of my selection, also the quantity of the selected, then my question is how can i see every checkbox checked from all the rows and only show the checked ones. how could i make in the next code to list all the checked boxes with their own row name and the quantity? thanks you all.

    if(isset($_POST['enviarcomanda'])){
      echo "<h1>Valida la teva comanda!</h1>";

    }else{
      echo "";
    }

Answer



Solution:

Depending on how you are saving the value of the checkbox, you would check to see if that value is set on the return parse. If it is set, then echo out the value of the returned $row within an input that is set to checked.

My example below uses an array where the values are set to true/false

NOTE: I am using an associative array that saves the name as the key and the value as the value, so take into consideration how you are saving your values as your return will likely have a number as key and the value as value.


//--> where $row represents your query return...
$row = array(
  "nom" => array(
    "Jessie" => true,
    "Mike" => false,
    "Ben" => false,
    "Jake" => false,
    "Bill" => false,
    "John" => true
  )
);

$stmt = null;
//--> foreach loop to gather key/value pairs for display within conditional
foreach($row['nom'] as $name => $value){   
  //--> conditional to check if the set value returned from query is set to the expected value 
  //--> Again, I used 'true' in my example, set this to your expected value  
  if($value === true){
    $stmt .= '<span>'. $name .'<input type="checkbox" name="'.$name.'" checked></span>';
  }else{
    $stmt .= '<span>'. $name .'<input type="checkbox" name="'.$name.'"></span>';
  }      
}

OUTPUT:

Echo out the $stmt variable where you want to display the span tags with the proper formatted check boxes. Format the CSS and HTML as you prefer in your code. With in HTML: <?=$stmt?>

enter image description here

Source