homepage Welcome to WebmasterWorld Guest from 50.16.112.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Undefined index error, what does it mean?
Jamier101



 
Msg#: 4285580 posted 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>

 

mvaz

5+ Year Member



 
Msg#: 4285580 posted 1:21 pm on Mar 22, 2011 (gmt 0)

You have missed the semicolon after print "$answer"

Should be print "$answer";

Jamier101



 
Msg#: 4285580 posted 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

IntegrityWebDev



 
Msg#: 4285580 posted 3:05 pm on Mar 22, 2011 (gmt 0)

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

Jamier101



 
Msg#: 4285580 posted 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.

Jamier101



 
Msg#: 4285580 posted 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.

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4285580 posted 4:18 pm on Mar 22, 2011 (gmt 0)

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

Jamier101



 
Msg#: 4285580 posted 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.

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4285580 posted 5:31 pm on Mar 22, 2011 (gmt 0)

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

Jamier101



 
Msg#: 4285580 posted 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.

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4285580 posted 12:34 pm on Mar 23, 2011 (gmt 0)


<?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

Jamier101



 
Msg#: 4285580 posted 1:52 pm on Mar 23, 2011 (gmt 0)

Oh dear, I get "Something went wrong".

Jamier101



 
Msg#: 4285580 posted 1:52 pm on Mar 23, 2011 (gmt 0)

Oh dear, I get "Something went wrong".

Matthew1980

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4285580 posted 2:54 pm on Mar 23, 2011 (gmt 0)

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

Alter that line...

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

Cheers,
MRb

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved