Page 1 of 1

Undefined Offset

Posted: Fri Jan 15, 2010 12:07 pm
by michaelk46
have this script (below) that works great, but it generates a notice when it runs.... Notice: Undefined offset: 3 in C:\wamp\www\test.php on line 14 (line 14 refers to line 13 in the below code)

Code: Select all

 
if ($_SERVER['REQUEST_METHOD'] != "POST")
    {
        include 'test.html.php';
    }
else
    {
        $a=0;
        $check = $_POST['check'];
        $number = count($check);
        while ($a <= $number)
            {
                print_r ($check[$a]);
                ++$a;
            }
    }
 
Here is the form code:

Code: Select all

 
<form action="" method="POST">
<input type="checkbox" name="check[]" value="1"  />
<input type="checkbox" name="check[]" value="2"  />
<input type="checkbox" name="check[]" value="3"  />
<input type="submit" name="export" value="Export to CSV"/>
</form>
 
How could I modify it so that it doesn't come up?

Re: Undefined Offset

Posted: Fri Jan 15, 2010 12:19 pm
by Reviresco
The array has 3 items, so the index only goes up to 2.

Change:

Code: Select all

while ($a <= $number)
To:

Code: Select all

while ($a < $number)

Re: Undefined Offset

Posted: Fri Jan 15, 2010 12:23 pm
by michaelk46
Thank You... That was easy