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>
Have you tried it
<?php echo $ano ? $ano : "" ?>
– Sergio
No, I’ll test it right now
– Nikk 17016
you’re talking to put this in the input?
– Nikk 17016
I just tried it didn’t work out ;-;
– Nikk 17016
Maybe I didn’t get it right. Where it comes from
undefined
? appears "Undefined" by text in input?– Sergio
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
– Nikk 17016
These are the messages that appear
– Nikk 17016
And Undefined’s mistake, not if it’s not forgiveness ;-;
– Nikk 17016
Your $_POST['datac'] is not set at the beginning as it has not been given Ubmit in the form yet.
– André Vicente