dimanche 17 janvier 2021

How to echo checkbox values as integers and not strings?

Below is the function that spits out a list of checkboxes.

function showClass($id_staff, $id_subject){
$class = getClass($id_staff, $id_subject);
$id = getClassId($id_staff, $id_subject);

for($i = 0; $i < count($class); $i++){ 
             
echo "<input type='checkbox' name = 'checklist_class[]'  value=".$id[$i].">";  //$id[$i] is saved as string and i want to save it as int
echo $class[$i];
 }
}

But when I extract the value $id[$i] it saves as a string and not integer. When i execute the code below, using the $id as an int it doesn't do anything. But when I use it as a string it works. I tried casting and intval() but it still doesn't work.

if(!empty($_POST['checklist_class'])) {                  
    foreach($_POST['checklist_class'] as $id) {
       //stuff that uses $id as an int
    }
 }



Aucun commentaire:

Enregistrer un commentaire