Having Error: "Undefined index"

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
wallstreet
Forum Newbie
Posts: 6
Joined: Tue May 29, 2007 8:07 am

Having Error: "Undefined index"

Post by wallstreet »

Hello,

Only for some users there is the following error:

PHP Notice: index: view in C:/Domains/bars-net.com/wwwroot/iframes/photos-view.php on line 4

Appears on this page: http://my-caricature.com/bars/photos.php (hebrew page) and some other pages.

What is that error means and how can I solve it?
Thanks!
Eyal
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
$arr = array(
  'index1'=>'val1',
  'index3'=>'val3'
);

echo $arr['index1'];  // <- ok, index1 exists in $arr
echo $arr['indexXYZ'];  // <- notice: undefined index indexXYZ

What's on line 4 in photos-view.php ?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Well, whats the first few lines of code in in that file?
wallstreet
Forum Newbie
Posts: 6
Joined: Tue May 29, 2007 8:07 am

Post by wallstreet »

Here is the entire photos-view.php page (Hebrew text translated to english):

Code: Select all

<?
$view=$_GET["view"];
?>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">

<body bgcolor=black background="../images/photos/boxbg.jpg">

<?
if ($view=="") {
	?><br><br><div dir=rtl><center><font face=verdana size=2 color=white>The view a larger photo here click on the small version of the photo on the right iframe</div><?
} else {
	?>
	
	<br><br>
	<center>
	<img src="../images/photos/m<? echo $view; ?>.jpg" width=281 height=221 style="border: 1px solid #cacaca;">
	<br><br>
	<center>
	<a href="../images/photos/<? echo $view; ?>.jpg" target="Sdf"><font face=verdana size=2 color=white>Enlarge to full screen</a><br><br></a>
	<a href="javascript:void(0);" onClick="showme();"><font face=verdana size=2 color=white><div dir=rtl><b>Contact Us!</div></a>
	<?
}
?>

<script>
function showme() {
	parent.location='../contact.php?text=photo';
}
</script>
This page, which is inside an iframe, is reciving a photos from the iframe on the right via GET method.
The $view var is the name of the ID of the photo.
If there is nothing inside $view, then it's show's a text "please click on some photo".

Thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I see only one access to an array element. But it's on line 2 not 4 as the warning message states.

Anyway, please try

Code: Select all

<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<body bgcolor=black background="../images/photos/boxbg.jpg">
<?php
if ( empty($_GET["view"]) ) {
  ?><br><br><div dir=rtl><center><font face=verdana size=2 color=white>The view a larger photo here click on the small version of the photo on the right iframe</div><?php
} else {
?>
  <br><br>
  <center>
  <img src="../images/photos/m<? echo $_GET['view']; ?>.jpg" width=281 height=221 style="border: 1px solid #cacaca;">
  <br><br>
  <center>
  <a href="../images/photos/<? echo $_GET['view']; ?>.jpg" target="Sdf"><font face=verdana size=2 color=white>Enlarge to full screen</a><br><br></a>
  <a href="javascript:void(0);" onClick="showme();"><font face=verdana size=2 color=white><div dir=rtl><b>Contact Us!</div></a>
<?php
}
?>

<script>
function showme() {
  parent.location='../contact.php?text=photo';
}
</script>
User avatar
Gente
Forum Contributor
Posts: 252
Joined: Wed Jun 13, 2007 9:43 am
Location: Ukraine, Kharkov
Contact:

Post by Gente »

If 'view' parameter isn't set in your URL 'undefined index' message will appear every time when you'll try to use $_GET['view'].
Use following code as example

Code: Select all

if (isset($_GET['view']))
{
  $view = '';
}
else
{
  $view = $_GET['view'];
}
or this one

Code: Select all

$view = (isset($_GET['view'])) ? $_GET['view'] : '';
Post Reply