Notice undefined index PHP error

0

I have a problem with my code, it turns out that when I give a value to my variable $ month, I get it all right, and I have since in the case that $ month has a null value or is empty, take me out the current month, and I get it right. The problem is that in the upper part I get the message of Undefined index: id_mes , although I think I do not require it since I have put the isset and! Empty, I get it all right but the message comes up there, and not I understand why.

Let's see if someone can help me by taking a look, because I can not see it, really.

<script src="js/semanal.js"></script>

<?php
if (strpos(getcwd(), 'apen_files') !== false) {
	define('_PS_ADMIN_DIR_', getcwd());
} else {
	define('_PS_ADMIN_DIR_', getcwd().'/includes/apen_files/');
}
 require (_PS_ADMIN_DIR_."/config.php"); 
 
 //-----------------------------------------------------
 $any = $_POST['id_anio']; 
 $mesac=date ("F");
$mes = $_POST['id_mes'];
$nomdia=date ("l");
$dia = $_POST['id_day'];
$date= "".$dia."-".$mes."-".$any;
 ?>
<link rel="stylesheet" type="text/css" href="../../css/estilo.css"/>
<div id="principal_pordias">
	
</div>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<?php $meses = array("ENERO", "FEBRERO", "MARZO", "ABRIL", "MAYO", "JUNIO", "JULIO", "AGOSTO", "SEPTIEMBRE", "OCTUBRE", "NOVIEMBRE", "DICIEMBRE"); ?>
<?php $dias = array('', 'LUNES','MARTES','MIÉRCOLES','JUEVES','VIERNES','SÁBADO', 'DOMINGO'); ?>
<?php function saber_dia($date) {
$dias = array('', 'LUNES','MARTES','MIÉRCOLES','JUEVES','VIERNES','SÁBADO', 'DOMINGO');
$fecha = $dias[date('N', strtotime($date))];
echo $fecha;
} ?>
<p>
<div><center>
<?php echo $any; ?>
</center></div>
<p>
<div><center>
<?php
if ($mes && !empty($mes)) {
    echo $meses[$mes -1];
}
else {
	echo $meses[date('n')-1];
}
?>
</center></div>
<p>
<div><center>
<?php echo $dia; ?>
</center></div>
<p>
<div><center>
<?php
if ($mes && !empty($mes)) {
saber_dia($date); }
else {
	echo $dias[date('w')];
}
?>
</div></center>
    <div id="fecha"></div>
</body>

</html> 

Thank you very much for your help one more time.

My AJAX code from which I receive the variables 'id':

function select_pordias(dia, anio, mes) { 
			 $.ajax({
                    type: 'post',
                    url: 'includes/apen_files/recarga_agenda_pordias.php',
                    data: {
                    	id_anio:anio,
						id_mes:mes,
						id_day:dia
                        },
                    success: function (response) {
						//alert (response);
                        $('#body_center').html(response);
                        }
              });
			  
	}

if(i<primerDiaSemana)
		{
			// celda mes anterior y siguiente
			resultado+="<td class='ayer'><a class='ayer' onclick='select_pordias(this.id,"+year+","+month+")'>"+(ultimoDiaMesAnt - (primerDiaSemana - i - 1))+"</a></td>";
			
		} else if (i>=last_cell) {
		if (i % 7 == 0) {
		                resultado+="<td class='post' style='color: #fd9292'><a class='red' onclick='select_pordias(this.id,"+year+","+month+")'>"+a+++"</a></td>";
                  } else {
                    resultado+="<td class='post' style='color: #cccccc'><a class='post' onclick='select_pordias(this.id,"+year+","+month+")'>"+a+++"</a></td>";
                  }
				  }
		 else{
			// mostramos el dia
			if(dia==actual.getDate() && month==actual.getMonth()+1 && year==actual.getFullYear())
				resultado+="<td class='hoy' ><a class='hoy' onclick='select_pordias(this.id,"+year+","+month+")' id="+ dia +">"+dia+"</a></td>";
			else
				resultado+="<td><a class='negro' onclick='select_pordias(this.id,"+year+","+month+")' id="+ dia +">"+dia+"</a></td>";
			dia++;
		}
    
asked by Joume Parra 20.06.2018 в 18:16
source

2 answers

1

It is not enough to put:

if ($mes && !empty($mes)) {
    echo $meses[$mes -1];
}

If you want to avoid the error, you would have to put, at the beginning:

$mes = isset($_POST['id_mes'])? $_POST['id_mes'] : null;

or something similar.

    
answered by 21.06.2018 / 14:24
source
0
$mes = $_POST['id_mes'];

check if that varible 'id_mes' that you receive in $_POST is defined

    
answered by 20.06.2018 в 18:37