how do I get all checkbox variables even if not checked from HTML to PHP?

前端 未结 5 1130
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 17:39

I noticed that PHP seems to return only values of checked checkboxes. I would like to see a list of checkboxes, not just values of checked checkboxes. Is there a way to dete

5条回答
  •  -上瘾入骨i
    2020-12-02 18:06

    Suppose you have a 3 checkboxes you want to check, and update_settings is the name of your functions that take the checkbox name as a first argument and a bool value as a second one (activate or not).

    Take the following snippet:

    $checkboxes = array("checkbox1", "checkbox2", "checkbox3");
    foreach($checkboxes as $checkbox){
        $checked = isset($_POST[$checkbox]);
        update_settings($checkbox, $checked);
    }
    

    Althouth Peter Kovacs solution it's going to work, I don't think it's practical since you can already check your variables using isset.

提交回复
热议问题