Notice: Undefined index:


#1

hi,

can one anyone provide some insight as to why i’m getting the following message?

Notice: Undefined index: id in D:about.php on line 25

This is line 25:

<? $thepage = $_GET["id"]; ?>

thanks in advance.


#2

http://us2.php.net/manual/en/ref.errorf … -reporting

Check the php.ini file on your server for an “error_reporting” setting. The default value should be: E_ALL & ~E_NOTICE which excludes “notice” messages from being output.

Whats thepage??? should it be about.php


#3

i changed the php.ini file as you suggested and the notice is gone.

yes, the page is about.php.

thanks for your help… and thanks for your reply on my other post. i appreciate it.


#4

You should always post a couple of lines of code from above and below the error. PHP errors, if you learn to understand them, say usually the line above or below the actual line it gives.

If anyone cares… The reason is, say you have this…

Line 6 - $h = 25
Line 7 - $x = 3;

Well this would cause php to through the error -
Parse error: syntax error, unexpected T_VARIABLE in error.php on line 7

Why?? Because you never told PHP that there was the end of a line. So to the parser it looks like this…

Line 6 $h = 25$x = 3;

For php there technically is no line 7 as it didn’t know that line 6 had ended.

Now back to your problem – You can also user error_report(“E_ALL”); at the top of you page. Search at http://www.php.net for all the settings to this and you can specify the level of error reporting the parser will do.


#5

As some background information to the error you got: apparently the index name ‘id’ doesn’t exist in the array $_GET (so, $_GET[‘id’] has not been assigned a value). This is a warning of type E_NOTICE, but it is strongly recommended to make sure variables are initiated before they’re being used:

[php]if (isset($_GET[‘id’])) { $thepage = $_GET[‘id’]; }[/php]

The reason behind this is to prevent automatic variables from being unintentionally initiated by a malicious user (I believe the correct term would be ‘script insertion’), thus posing a security hole to your webserver. $_GET fetches everything from the URL query though, so it’s kind of safe to not initiate its indexes, but it’s good that PHP is so indiscriminate about it :slight_smile:

On a sidenote: be sure to check the value of $_GET, $_POST and $_COOKIE variables before you use them :wink:


#6

thanks, i think i’m making progress.

i’m now getting the message:
Notice: Undefined variable: thepage in C:AccountsenockdeswwwRootabout.php on line 58

i’m not sure how do define a variable, i tried a few things like:
var = $thepage;
$thepage = $HTTP_POST_VARS[“thepage”];

thanks.

this is most of the code…


[php]
<? if (isset($_GET['id'])) { $thepage = $_GET['id']; } ?>

	<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#D6D6D6">
<tr>
	<td height="26" align="center" valign="middle">
	
	<table border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#D6D6D6">
		<tr>
			<td width="90" align="center" class="sborder"><a href="about.php?id=0" class="navsub" <?php If (($thePage == "") or ($thePage == "0")){?>style="color: #454545; font-weight: bold;"<? } else {?>><? } ?>About Us</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>
			<td width="90" align="center" class="sborder"><a href="about.php?id=3" class="navsub" <?php If ($thePage == "3") {?>style="color: #454545; font-weight: bold;"<? } else {?>><? } ?>News</a></td>
		</tr>
	</table>
			</td>
</tr>
<? switch ($thepage) { case "" || "0": include("about_intro.html"); break; case "1": include("about_clients.html"); break; case "2": include("about_bio.html"); break; case "3": include("about_news.html"); } ?>

[/php]

Admin Edit: [php] tags added for readability. Please refer to http://phphelp.com/guidelines.php for posting guidelines.


#7

Well, since the if statement may evaluate ‘false’, the variable ‘thepage’ may never be initialized :wink: As I said, you should always initialize variables (but this time, it’s not $_GET[‘id’] I’m talking about):

[php]

<?php $thepage = ""; if (isset($_GET['id'])) { $thepage = $_GET['id']; } [/php]

#8

so my “case/switch” is working, so that must mean the PHP is reading the variable ?id=1 in my URL, correct?

but my If Else statement is not working:

<?php If ($thePage == "3") {?>

I’m confused how the case/switch is working but not my if/else. I’m guessing the syntax of my if/else is not correct? though, i do not get any errors on my page.

thanks again for the help.


#9

I’m thinking PHP is throwing a hissy over case sensitivity: try ‘if’ instead of ‘If’.


#10

i made the “if” lowercase, and no luck.

<a href="about.php?id=3" class="navsub"<?php if ($thePage == "3") {?> style="color: #454545; font-weight: bold;"><? } else {?>><? } ?>News</a>

#11

Just out of curiosity, why aren’t you doing something like this:

[php]

<?php $styleAttr = ""; if ($thePage == 3) { $styleAttr = " style="color: #454545; font-weight: bold;""; } echo "News"; ?>

[/php]


#12

@Zyppora

I saw your recommendation on my other post.
having the HTML and PHP on one line seems simpler “to me”, and prefer not to repeat the href tag… but i’m not sure why it is not working.

so i coded it like this (and it is working):

[php]<? $thepage = “”;
$styleAttr = “”;
$styleAttr = " style=“color: #454545; font-weight: bold;”";

		if (isset($_GET['id'])) { $thepage = $_GET['id']; } 
	?>
	
	
	<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#D6D6D6">
<tr>
	<td height="26" align="center" valign="middle">
	
	<table border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#D6D6D6">
		<tr>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=0" class="navsub"
			<?php if (($thepage == 0) || ($thepage == "")){
			echo "".$styleAttr."";
			}?>
			>about Us</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=3" class="navsub"
			<?php if ($thepage == 3){
			echo "".$styleAttr."";
			}?>
			>News</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>         
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=1" class="navsub"
			<?php if ($thepage == 1){
			echo "".$styleAttr."";
			}?>
			>Clients</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=2" class="navsub"
			<?php if ($thepage == 2){
			echo "".$styleAttr."";
			}?>
			>Bio</a></td>
		</tr>
	</table>[/php]

the only thing i don’t like, and maybe it does not matter is that the parsed HTML looks like this:

[code]

		<tr>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=0" class="navsub"
							>about Us</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=3" class="navsub"
							>News</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>         
			<td width="90" align="center" class="sborder">

			<a href="about.php?id=1" class="navsub"
							>Clients</a></td>
			<td><img src="images/spacer.gif" width="4" height="1" border="0" alt=""></td>
			<td width="90" align="center" class="sborder">
			<a href="about.php?id=2" class="navsub"
			 style="color: #454545; font-weight: bold;"				>Bio</a></td>
		</tr>
	</table>[/code]

what are your thoughts? thanks.


#13

My thoughts: you forgot the PHP opening tags for your if statements :wink:


#14

That’s strange… the open tags are there, but when i wrap the code inside the “php” tag, they do not display in my post.

anyway. thanks for your help.

[php]












<a href=“about.php?id=0” class=“navsub”
<?php if (($thepage == 0) || ($thepage == “”)){
echo “”.$styleAttr."";
}?>
>About Us

<a href=“about.php?id=3” class=“navsub”
<?php if ($thepage == 3){
echo “”.$styleAttr."";
}?>
>News

<a href=“about.php?id=1” class=“navsub”
<?php if ($thepage == 1){
echo “”.$styleAttr."";
}?>
>Clients

<a href=“about.php?id=2” class=“navsub”
<?php if ($thepage == 2){
echo “”.$styleAttr."";
}?>
>Bio
[/php]