Jump to content

Why at the beginning keeps saying undefined index with a session variable


BroGad
Go to solution Solved by QuickOldCar,

Recommended Posts

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>

 

 

 

Link to comment
Share on other sites

  • Solution

Before trying to use something first check if it exists.

You are doing addition on the session values that won't exist the first time or until the first one was set using the form.

 

Can set them to a default value of zero the first view

<?php
session_start();
if (!isset($_SESSION['conted'])) {
$_SESSION['conted'] = 0;
}
if (!isset($_SESSION['contper'])) {
$_SESSION['contper'] = 0;
}
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'];
}
?>  
Edited by QuickOldCar
Link to comment
Share on other sites

As for clearing it can make a href link to a new script, whatever suits your needs...destroy the entire session with session_destroy(), unset any session variables, assign them zero values.

Then set a header to return to original referring location

 

<a href="reset.php">Reset</a>

 

reset.php examples

<?php
session_start();
session_destroy();
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
<?php
session_start();
unset($_SESSION['conted']);
unset($_SESSION['contper']);
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
<?php
session_start();
$_SESSION['conted'] = 0;
$_SESSION['contper'] = 0;
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.