Notice: Undefined offset: 5 in C: \ xampp \ htdocs \ My projects \ teste2.php on line 10, the value of the arrays being a value of a form

-1

I need to write a data chosen by the user, but when all data is not chosen this message appears Notice: Undefined offset: 5 in C: \ xampp \ htdocs \ My projects \ on line 10

asked by anonymous 19.07.2018 / 22:45

1 answer

1

Problem

The value of the checkbox not sent to the POST if it is not selected.

Solution

Make sure the value is set in the $ _POST array before using it.

    $nro_funcoes = 10;

    for ($id = 1; $id <= $nro_funcoes; $id++) {
        $nro_funcao[$id] = $id;
    }

    foreach ($nro_funcao as $func) {

        //verificar se está definido

        if (isset($_POST[$func])) {
            $funcao[$func] = $_POST[$func];
        }
    }

    $dados = implode(', ', array_filter($funcao));
    
19.07.2018 / 22:54