Welcome to WebmasterWorld Guest from 54.205.119.93

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Undefined index error, what does it mean?

   
10:48 am on Mar 22, 2011 (gmt 0)



I've been working on a simple calculator so that customer can work out how many square metres of flooring they require. I have the following scrip but keep getting an error:

Notice: Undefined index: length in D:\### on line 35

Notice: Undefined index: width in D:\### on line 36

I've never seen the error before so I'm sure what it means.

Converter.php
<body>
<div>
<form id="form1" name="form1" method="post" action="">
<table width="20%" border="0" cellspacing="0">
<tr>
<td>Width</td>
<td><label>
<input type="text" name="width" id="width" />
</label></td>
</tr>
<tr>
<td>Length</td>
<td><label>
<input type="text" name="length" id="length" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label></td>
<td>&nbsp;</td>
</tr>
</table>
</form>

<?php

$length = $_POST['length'];
$width = $_POST['width'];
$answer = $width * $length;
print "$answer"

?>

</div>

</body>
1:21 pm on Mar 22, 2011 (gmt 0)

5+ Year Member



You have missed the semicolon after print "$answer"

Should be print "$answer";
1:34 pm on Mar 22, 2011 (gmt 0)



I didn't notice that!

I've corrected it but when I upload the file I still get the same error, however once I've done my first calculation the error disappears... I don't get it! :-s
3:05 pm on Mar 22, 2011 (gmt 0)

5+ Year Member



It sounds like width and length aren't in the post before the first load so it's giving you this notice to let you know that.

Just off my head (and untested) you may want to do something like this:

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


...in place of those 2 lines.

Hopefully this will help.

BTW, it is good to properly fix this but keep in mind it is a NOTICE, and not a WARNING or an ERROR.

-Chris
3:58 pm on Mar 22, 2011 (gmt 0)



Thanks for that, I finally got it working with:


<?php

if (isset($_POST['length']))
{
$length = $_POST['length'];
}
if (isset($_POST['width']))
{
$width= $_POST['width'];
}
$answer = 'width' * 'length';
print "$answer";

?>


When nothing is in the field ie. 1st load, a 0 is displayed is there a way of making this not appear until I have done a calculation?

I've thought about using the empty() function but am unsure.
4:07 pm on Mar 22, 2011 (gmt 0)



Infact that doesn't cure the issue, it prevents the undefined index error statement from appearing but now the calculations are not conducted upon clicking submit.
4:18 pm on Mar 22, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there jamier101,

Your code:-

<?php

$length = $_POST['length'];
$width = $_POST['width'];
$answer = $width * $length;
print "$answer"

?>

What it needs to be in order for the error to go is this:-

<?php

$length = (isset($_POST['length']) ? strip_tags($_POST['length']) : '');
$width = (isset($_POST['width']) ? strip_tags($_POST['width']) : '');
$answer = $width * $length;
echo $answer;
?>

If that doesn't sort it out, I'll do something later on and explain in more detail, but at the moment, I can't dedicate too much time.

Definition: Undefined index error; this happens when you try and reference a variable or in this instance an element of an array that hasn't been given state/value yet.

Cheers,
MRb
5:02 pm on Mar 22, 2011 (gmt 0)



Thanks Matthew :-)


Definition: Undefined index error; this happens when you try and reference a variable or in this instance an element of an array that hasn't been given state/value yet.


It makes sense now why the error appeared, I just need to now figure out how to not display a zero when there is nothing in the boxes.
5:31 pm on Mar 22, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Where are you getting the zero appear? Pop the relevant current code into the thread and I will see what I can do, probably even tidy it up as I am all coffee'd up :)

On second read it looks like you need to make the math side of this conditional so that the sum/calculation will only occur IF the $_POST data is set, then if that's right $answer will only have state IF $_POST data is submitted.

You will find that it is $answer that is at 0 - at least that's how I read it.

Cheers,
MRb
8:46 am on Mar 23, 2011 (gmt 0)



Where are you getting the zero appear?


The zero is appearing because the $answer variable is empty.

The code I currently have is:

<?php

$length = (isset($_POST['length']) ? strip_tags($_POST['length']) : '');
$width = (isset($_POST['width']) ? strip_tags($_POST['width']) : '');
$answer = $width * $length;
echo $answer;

?>


I was thinking about using the empty() function but I'm not quite sure if a) if this is the right function b) how to go about using it.
12:34 pm on Mar 23, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member




<?php
if (isset($_POST['submit']) && !empty($_POST['submit'])){
$length = (isset($_POST['length']) ? strip_tags($_POST['length']) : '');
$width = (isset($_POST['width']) ? strip_tags($_POST['width']) : '');
$answer = $width * $length;
echo $answer;
}
else{
echo "Something went wrong";
exit;
}
?>


Form submission handler for you, rudimentary but functional, have a play see what you think, and yes, empty() was perfect for this.

Cheers,
MRb
1:52 pm on Mar 23, 2011 (gmt 0)



Oh dear, I get "Something went wrong".
1:52 pm on Mar 23, 2011 (gmt 0)



Oh dear, I get "Something went wrong".
2:54 pm on Mar 23, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



if (isset($_POST['submit']) && ($_POST['submit'] == "Submit")){

Alter that line...

(Sorry to be so succinct, but quite busy atm)

Cheers,
MRb