How to hide undefined or validated variables after reloading the page?

1

I have 2 variables that get the values of an input type month , but when the code is executed for the first time ie when the page loads, the variables are undefined and the code takes the pc date.

I would like to know if you can hide them, or make an if in js so that the variables are only validated after reloading the page.

 <body>

        <?php
//VARIAVEIS INDEFINIDAS
        $datee = explode('-', $_POST['datac']);
        $mes = $datee[1];
        $ano = $datee[0];
        $ultimo_dia = date("t", mktime(0, 0, 0, $mes, '01', $ano));
//----            

if ($mes == date('m')) {
            $dias = $ultimo_dia;
        } elseif ($mes == '') {
            $mes = date('m');
            $ano = date('o');
            $dias = $ultimo_dia;
        } else {
            $dias = $ultimo_dia;
        }
        
        ?>

        <form method="post" action="date.php">
            <input type="month" name="datac" value="<?php echo $ano ?>-<?php echo $mes ?>" required><input type="submit">
            <table class="table table-striped" width="210" border="2" cellspacing="4" cellpadding="4">
                <tr>
                    <td width="80px"><center>Domingo</center></td>
                <td width="80px"><center>Segunda</center></td>
                <td width="80px"><center>Terça</center></td>
                <td width="80px"><center>Quarta</center></td>
                <td width="80px"><center>Quinta</center></td>
                <td width="80px"><center>Sexta</center></td>
                <td width="80px"><center>Sábado</center></td>
                </tr>
<?php
echo "<tr>";
for ($i = 1; $i <= $dias; $i++) {
    $diadasemana = date("w", mktime(0, 0, 0, $mes, $i, $ano));
    $cont = 0;
    if ($i == 1) {
        while ($cont < $diadasemana) {
            echo "<td></td>";
            $cont++;
        }
    }
    echo "<td width='100px' height='100px'><center>";
    echo $i;
    echo "</center></td>";
    if ($diadasemana == 6) {
        echo "</tr>";
        echo "<tr>";
    }
}
echo "</tr>";
?>
            </table>


        </form>
    </body>
</html>
    
asked by anonymous 14.06.2016 / 14:35

1 answer

1

You are giving undefined that you do not already have $_POST , so what you have to do is check if you have POST. At the beginning of your code, put it this way:

if(isset($_POST)){  
     $datee = explode('-', $_POST['datac']);
     $mes = $datee[1];
     $ano = $datee[0];
     $ultimo_dia = date("t", mktime(0, 0, 0, $mes, '01', $ano));
}else{
     $mes = '';
     $ano = '';
}

If you are coming from POST you will set the variables $mes and $ano with the values, otherwise, it will set with ''.

    
14.06.2016 / 14:54