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

Asked

Viewed 558 times

1

I have 2 variables that receive the values of a input type Month, but when the code is first executed or when the page is loaded, the variables are Undefined and the code takes the date from the pc.

I wonder if it is possible to hide them , or make a if in the js so that 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>

  • 1

    Have you tried it <?php echo $ano ? $ano : "" ?>

  • No, I’ll test it right now

  • you’re talking to put this in the input?

  • I just tried it didn’t work out ;-;

  • Maybe I didn’t get it right. Where it comes from undefined? appears "Undefined" by text in input?

  • Notice: Undefined index: datac in C: xampp htdocs wallpaper phpfiles date.php on line XX Notice: Undefined offset: 1 in C: xampp htdocs wallpaper phpfiles date.php on line XX

  • These are the messages that appear

  • And Undefined’s mistake, not if it’s not forgiveness ;-;

  • 1

    Your $_POST['datac'] is not set at the beginning as it has not been given Ubmit in the form yet.

Show 4 more comments

1 answer

1


Is giving undefined which does not yet have the $_POST, then what you have to do is check if you have the POST. At the beginning of your code put 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 the POST vc will set the variables $mes and $ano with the values, otherwise , will set with ''.

  • error keeps persisting

  • The error happens after you give Ubmit or when you open the page? You can identify which line is giving the error?

  • when I open the page are these lines $datee = explode('-', $_POST['datac']);, $mes = $datee[1]; and $ultimo_dia = date("t", mktime(0, 0, 0, $mes, '01', $ano));

  • 3 errors : Notice: Undefined index: datac in C: xampp htdocs Wallpaper phpfiles date.php on line 19 Notice: Undefined offset: 1 in C: xampp htdocs Wallpaper phpfiles date.php on line 20 Warning: mktime() expects Parameter 6 to be long, string Given in C: xampp htdocs wallpaper phpfiles date.php on line 22

  • 1

    Even with isset($_POST) is he entering this part of the code? Exchange for isset($_POST['datac']).

  • Thanks now it worked

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.