Hello everyone,
I created a form for taking an order lunch.Everything works fine only i get an error on my php file on the array section.

Notice: Undefined index: drink in lunch.php
I don't see i have missed anything, but i hope someone might be able to spot my mistake.Please advice!

Thanks

<head>
     <meta charset = "utf-8"/>
	 <title>LUNCH ORDER</title>
	 <h1>WELCOME</h1>
</head>
<?php
$drinks = $_POST['drink'];
$drink1 = $drinks[0];
$drink2 = $drinks[1];

$first_name = $_POST['first_name'];

if(isset($_POST['first_name'])){
   $first_name =$_POST['first_name'];
   echo $first_name;
   
}else{
   $first_name = "unknown";
   echo $first_name;
}

$lunch =$_POST['lunch'];

if(isset($_POST['lunch'])){
   $lunch = $_POST['lunch'];
   
   echo '<br>'."You ordered".'<br>'.$lunch;

}else{
   $lunch = "unknown";
   
   echo $lunch;
}

if(isset($_POST['drink'])){
  $drinks = $_POST['drink'];
  foreach($drinks as $key =>$value){
    echo $key. ' = ' .$value. '<br>';
	}
  }else{
    echo '<br>'.'No drink selected.';
    

}

?>
</html>

Recommended Answers

All 9 Replies

Member Avatar for diafol

Where's your form code?

Undefined index errors normally come from variables being called for but don't necessarily exist yet.

I edited your code:

<?php
$drinks = $_POST['drink']; //here is your error, change to:
  if(isset($_POST['drink'])) {
    $drinks = $_POST['drink'];
      $drink1 = $drinks[0];
      $drink2 = $drinks[1];
}

if(isset($_POST['first_name'])) { $first_name = $_POST['first_name']; }

} else {
   $first_name = "unknown";
   echo $first_name;
}

Hope this clears things up for you.

commented: thanks +1

Please keep in mind that this is just a Notice, not a fatal error. It's not actually stopping your program. Your script will continue running. If it's still not doing what you want, it means there's a logic error elsewhere in the script.

All this Notice is doing is telling you that the index called drink does not exist for the array $_POST when you refer to it on line 7 (in programming terms, $_POST was not initialized). This will happen any time someone arrives at your page without submitting something through POST, since you're accessing the $_POST array.

See how further down the page your code checks for it with if(isset($_POST['drink'])){ ? That's a safety to see first if the variable was initialized before you assign it to another variable. stoopkid's response applied this check the first time.

Notices are useful for debugging, but when you launch your site you should make sure to turn them off. Read up on PHP's error reporting so you know the difference between a Notice, Warning, and an Error:
error_reporting()
ini_set('display_errors')

commented: thanks +1

Undefined index errors normally come from variables being called for but don't necessarily exist yet.

Thanks for your response!
I tried the code it gives me the type of drink which is good, but still they is an error.
Notice: Undefined offset: 1 in lunch.php on line 36

<html lang ="en">
<head>
     <meta charset = "utf-8"/>
	 <title>LUNCH ORDER</title>
	 <h1>WELCOME</h1>
</head>

<?php
$first_name = $_POST['first_name'];

if(isset($_POST['first_name'])){
   $first_name =$_POST['first_name'];
   echo $first_name;
   
}else{
   $first_name = "unknown";
   echo $first_name;
}

$lunch =$_POST['lunch'];

if(isset($_POST['lunch'])){
   $lunch = $_POST['lunch'];
   
   echo '<br>'."You ordered".'<br>'.$lunch;

}else{
   $lunch = "unknown";
   
   echo $lunch;
}
if(isset($_POST['drink'])) {
$drinks = $_POST['drink'];
$drink1 = $drinks[0];
$drink2 = $drinks[1];

  foreach($drinks as $key =>$value){
    echo  $value. '<br>';
	}
  }else{
    echo '<br>'.'No drink selected.';
    

}

?>
</html>
Member Avatar for diafol

This is what they've been saying - you can't use $_POST on first page load as it doesn't exist yet.

How about:

<?php
if(isset($_POST['first_name'])){
   $first_name = $_POST['first_name'];
   echo $first_name;
}else{
   $first_name = "unknown";
   echo $first_name;
}

if(isset($_POST['lunch'])){
   $lunch = $_POST['lunch'];
   echo '<br>'."You ordered".'<br>'.$lunch;
}else{
   $lunch = "unknown";
   echo $lunch;
}

if(isset($_POST['drink'])) {
	$drinks = $_POST['drink'];
	
	$drink1 = $drinks[0]; //why this?
	$drink2 = $drinks[1]; //and this?

  	foreach($drinks as $drink){
    	     echo  $drink. '<br>';
	}
}else{
   echo '<br>'.'No drink selected.';
}?>
commented: thanks, works! +1

@ardav: that doesn't necessarily help either. You don't know if "drink" really is an array.
Additionally, I'd suggest to check that as well:

...
if(isset($_POST['drink'])) {
	$drinks = $_POST['drink'];
	if(is_array($drinks) && count($drinks)>=2){
	...

then you can be sure not run into a memory leak.

cu, Simon

commented: good spot +14
Member Avatar for diafol

quite right sDJh the drink loop doesn't help - but at least taking out the uninitialized lines does. :)

You can actually force the $_POST to be an array, which will probably be better:

if(isset($_POST['drink'])) {
	$drinks = (array) $_POST['drink'];
 
	//$drink1 = $drinks[0];
	//$drink2 = $drinks[1];
 
  	foreach($drinks as $drink){
    	     echo  $drink. '<br>';
	}
}else{
   echo '<br>'.'No drink selected.';
}

Then you don't have to worry if there's just one or a hundred.

Hi,
Thanks for your post, it worked fine after removing

$drink1 = $drinks[0]; //why this?
      $drink2 = $drinks[1]; //and this?

and adding this bit foreach($drinks as $drink){

Thanks again!

This is what they've been saying - you can't use $_POST on first page load as it doesn't exist yet.

How about:

<?php
if(isset($_POST['first_name'])){
   $first_name = $_POST['first_name'];
   echo $first_name;
}else{
   $first_name = "unknown";
   echo $first_name;
}

if(isset($_POST['lunch'])){
   $lunch = $_POST['lunch'];
   echo '<br>'."You ordered".'<br>'.$lunch;
}else{
   $lunch = "unknown";
   echo $lunch;
}

if(isset($_POST['drink'])) {
	$drinks = $_POST['drink'];
	
	$drink1 = $drinks[0]; //why this?
	$drink2 = $drinks[1]; //and this?

  	foreach($drinks as $drink){
    	     echo  $drink. '<br>';
	}
}else{
   echo '<br>'.'No drink selected.';
}?>

Undefined index errors normally come from variables being called for but don't necessarily exist yet. So put all $_GET, $_POST values into a local variable and validate the empty condition, like  $drinks = $_POST;

Alternatively you can set the error reporting off on the server like  

<?php error_reporting(0);   //Disable error reporting ?>
Put the above line of code at the top of the page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.