Undefined Index Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hsiwux07
Forum Newbie
Posts: 9
Joined: Mon Oct 16, 2006 3:05 am

Undefined Index Error

Post by hsiwux07 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm trying to basic PHP code here but I got this error 

Undefined Index Error.

Would somebody give me some hints about where to correct to make this code working fine without any error ?

here is the code

Code: Select all

<?php

switch($_REQUEST['opt'])
{
case "pro":
echo "Hi";
break;

default:
$say="hi";
echo <<<FORM
<form action="Combine.php" method="POST">
<input type="hidden" name="opt" value="pro">
<input type="submit" value="PRESS ME">
</FORM>
FORM;
break;
}
?>

Thanks a lot !


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

The first time you run the code no form value has been input therefore $_REQUEST['opt'] does not exist.

I would simple use an if then else statement to check if $_POST has been sent. If it has then process with a switch if you have more than one option. with the else outputting the form when the form has not been sent.
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Re: Undefined Index Error

Post by ryuuka »

hsiwux07 wrote:

Code: Select all

<?php

switch($_REQUEST['opt'])
{
case "pro":
echo "Hi";
break;

default:
$say="hi";
echo <<<FORM
<form action="Combine.php" method="POST">
<input type="hidden" name="opt" value="pro">
<input type="submit" value="PRESS ME">
</FORM>
FORM;
break;
}
?>
what a better solution might be:

Code: Select all

echo ('

<<<FORM
<form action="Combine.php" method="POST">
<input type="hidden" name="opt" value="pro">
<input type="submit" value="PRESS ME">
</FORM>
FORM
');
note the ')

and you may also want to put the start and finish for the form outside the loop.

also what is this for? to my knowledge you can leave this out:

Code: Select all

<<<FORM

FORM
hope it helps GL
Last edited by ryuuka on Mon Oct 16, 2006 3:30 am, edited 1 time in total.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Undefined Index Error

Post by Luke »

It's because you are trying to use $_REQUEST['opt'] before it's defined... either check for a value, and supply a default value if it's not set...

Code: Select all

<?php
$opt = isset($_REQUEST['opt']) ? $_REQUEST['opt'] : null;

switch($opt){
	case "pro":
		echo "Hi";
		break;
	default:
		$say="hi";
		echo <<<FORM
		<form action="Combine.php" method="POST">
		<input type="hidden" name="opt" value="pro">
		<input type="submit" value="PRESS ME">
		</FORM>
FORM;
		break;
}
?>
Or enforce that $_REQUEST['opt'] must be set before doing anything with it at all

Code: Select all

<?php
if(isset($_REQUEST['opt'])){

switch($_REQUEST['opt']){
	case "pro":
		echo "Hi";
		break;
	default:
		$say="hi";
		echo <<<FORM
		<form action="Combine.php" method="POST">
		<input type="hidden" name="opt" value="pro">
		<input type="submit" value="PRESS ME">
		</FORM>
FORM;
		break;
}
?>
Also... why are you using $_REQUEST anyway? You're posting the form, so why not use $_POST instead??

EDIT: WOW, you guys are fast!
ryuuka wrote: also what is this for? to my knowledge you can leave this out:

Code: Select all

<<<FORM

FORM
hope it helps GL
It's called heredoc
hsiwux07
Forum Newbie
Posts: 9
Joined: Mon Oct 16, 2006 3:05 am

Post by hsiwux07 »

Thanks for the help, the problem is solved.


Also learned a new built-in function, isset() ... thanks a lot.


Allen
hsiwux07
Forum Newbie
Posts: 9
Joined: Mon Oct 16, 2006 3:05 am

Post by hsiwux07 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm sorry guys, I have an additional enquiries about expending this code.

If I have a list of code as following:

Code: Select all

<?php
switch()
{
case option1:
defined_function(1);
break;

case option2:
defined_function(2);
break;

case option3:
defined_function(3);
break;
}

function defined_function(1);
{
code;
}

function defined_function(2);
{
code;
}

function defined_function(3);
{
code;
}

?>

<html>
#ALL CONTENTS INSIDE#
</htmL>

If I have this coding structure, would I get the same error message with my initial execution ??

Since this reads the first line which is the first switch option, it should have an Undefined Index ?


Thanks.


Allen


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I don't understand your question... what are you trying to do?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You will always get an undefined index notice when you attempt to use a var that a) has an index and b) the index has not yet been given a value. Imagine this code:

Code: Select all

<?php
echo $myarray['myindex'];
?>
What is the value of $myarray['myindex']? Who knows? The fact that there is not known value for it is what the PHP engine is squawking about. Now look at this code:

Code: Select all

<?php
if (isset($myarray['myindex']))
{
    echo $myarray['myindex'];
}
else
{
    echo 'The myindex index is not set yet!';
}
?>
Now the PHP Engine knows to check to see if there is a known value, and, if there is, echo that value. Otherwise, echo something else.

Always check if a value is set before comparing/using it in your code to prevent undefined '?' notices (do not rely on having display errors off).
Post Reply