Small PHP issue: Undefined Index

Alex

New Member
I'm getting an undefined index on lines 15-19. The code works fine and I can disable the error messages, but I'm wondering why.

PHP:
<?php
session_start();

if (($_SESSION)==NULL){
 echo "Welcome, guest";
 echo "   <a href='login.php'>Login</a>";
 $postedby = "Guest";
}
else{
	echo "Welcome, " . $_SESSION['username'];
	echo "   <a href='logout.php'>Logout</a>";
	$postedby = $_SESSION['username'];
}

$submit = $_POST['submit'];
$heading=$_POST['heading'];
$details = $_POST['details'];
$section = $_POST['section'];
$location = $_POST['location'];
$contactinfo = $_POST['contactinfo'];
$date = date("Y-m-d");


if ($submit){
	if ($heading&&$details&&$section){
		include"config.php";
		$sql = "INSERT into posts (heading, details, location, section, date, postedby, contactinfo)"
				."values ('$heading', '$details', '$location', '$section', '$date', '$postedby', '$contactinfo')";	
		$result = mysql_query($sql);
		if($result){
			header('Location: successpost.php');		
		}
		else{
			echo "Please fill out all the fields";
		}
}
}
?>
<html>
<head>
<title>LessonWorld</title>

</head>

<body>
<form action="post.php" method="POST">
	<table>
		<tr>	
			<td>
			Ad title:
			</td>
			<td>
			<input type='text' name='heading'>
			</td>
		</tr>
		<tr>	
			<td>
			Ad content:
			</td>
			<td>

			<script language="javascript" type="text/javascript">
	function limitText(limitField, limitCount, limitNum) {
		if (limitField.value.length > limitNum) {
			limitField.value = limitField.value.substring(0, limitNum);
		} else {
			limitCount.value = limitNum - limitField.value.length;
		}
	}
	</script>
	<textarea name="details" rows="6" cols="60" onKeyDown="limitText(this.form.details,this.form.countdown,1000);" 
	onKeyUp="limitText(this.form.details,this.form.countdown,100 rows="10" cols="60");">
	</textarea><br>
	<font size="1">(Maximum characters: 1000)<br>
	You have <input readonly type="text" name="countdown" size="3" value="1000"> characters left.</font>
	</form>
			</td>
		</tr>
		<tr>	
			<td>
			Location:
			</td>
			<td>
			<input type='text' name='location'>
			</td>
		</tr>
		<tr>	
			<td>
			Specify how you wish to be contacted:
			</td>
			<td>
			<input type='text' name='contactinfo'>
			</td>
		</tr>
		<tr>	
			<td>
			Select section:
			</td>
			<td>
			<select name="section">
			<option value="Hockey Lessons" selected="selected">Hockey Lessons</option> 
			<option value="Football Lessons" selected="selected">Football Lessons</option> 
			<option value="Ping-pong Lessons" selected="selected">Ping-pong Lessons</option> 
			</td>
		</tr>
	</table>
	<input type='submit' name='submit' value='Post ad'>
</form>
</body>
</form>
</html>

Same issue here on lines 16-17
PHP:
<!DOCTYPE html>
<html>
<head>
<title>LessonWorld</title>
</head>

<body>

<p>Do you agree with the following <a href="terms.html">terms</a>?</p>
<form action="agreement.php" method="POST">
<input type="checkbox" name="agree" value="accept" /> Yes<br />
<input type="submit" name='submit' value="continue" >
</form>
</html>
<?php
$agree = ($_POST['agree']);
$submit = $_POST['submit'];

if ($submit){
	if( isset($_POST['agree']) )
	{
		header('Location: post.php');
	}
	else if (! isset($_POST['agree']) )
	{
		echo "You must accept to continue.";
	}

}
	
?>
Thank you!
 
Just be sure your code is trying to process the $_POST variables after submitting the form, not before. Also, you can't change the header location:
Code:
 header('Location: successpost.php');
after sending any output to the browser with HTML code or with PHP echo.

For future PHP issues, you might want to post them in the "Programming" forum.
 

conor

New Member
also another usefull trick is phps built in error suppressor - the at (@) symbol. See http://www.php.net/manual/en/language.operators.errorcontrol.php.

You can put this symbol before variables or functions to suppress any errors that should be created. I usually use it for form validation as it doesn't really matter if the variable is set, just that it has a value. Here's a quick example:

Code:
$submit = @$_POST[ 'submit' ];
 
Top