Why at the beginning keeps saying undefined index with a session variable
Hello. I want to calculate the average age of the people that submit the form. So I got 2 counters (I apologize for my english). One that stores the addition of the age and the other one that stores the number of people that submit the form. Then I need to calculate the average.Everytime I load the page for the first time it says "undefined index conted" and "undefined index contper".
But it does what I want: it shows the correct addition of the age and the counting of the people that submit the form. The error appears only when I load the page the first time. Then everything goes well.
Also I wanted to know how to delete those values stored in the session variables with a button, so when I hit the button displays the average and stops counting. (I apologize for my english) Thanks. This is my code:
<?php
session_start();
if(isset($_POST['btn1'])) {
$_SESSION['conted'] = $_SESSION['conted'] + $_REQUEST['ed'];
$_SESSION['contper'] = $_SESSION['contper'] + 1;
echo "El contador de edad va en " . $_SESSION['conted'];
echo '<br>';
echo "El contador de personas va en " . $_SESSION['contper'];
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" >
<title>Encuesta</title>
</head>
<body>
<form action="encuesta.php" method="post">
<table>
<tr>
<td>Nombre</td><td><input type="text" placeholder="Nombre" name="nom"></td>
</tr>
<tr>
<td>Apellido</td><td><input type="text" placeholder="Apellido" name="ape"></td>
</tr>
<tr>
<td>Edad</td><td><input type="text" placeholder="Edad" name="ed"></td>
</tr>
<tr>
<td>Dirección</td><td><input type="text" placeholder="Dirección" name="dir"></td>
</tr>
<tr>
<td>Barrio</td><td><input type="text" placeholder="Barrio" name="bar"></td>
</tr>
<tr>
<td>Teléfono</td><td><input type="text" placeholder="Teléfono" name="tel"></td>
</tr>
<tr>
<td colspan = 2 align="center"><input type="submit" value="Procesar" name="btn1"></td>
</tr>
<tr>
<td colspan = 2 align="center"><input type="submit" value="Terminar" name="btn2"></td>
</tr>
</table>
</form>
</body>
</html>