tags:

views:

53

answers:

2

With error_reporting(E_ALL); removed, my script works fine, however when I uncomment it, the following notice appears:

Notice: Undefined variable: messages in /home/www/test/register/html/form_1.html.php on line 11

On line 11, there is a function, which basically iterates over an array argument:

function displayMessages($array)

{
    if (!empty($array) && isset($array))
    {
        foreach ($array as $number => $error)
        {
            echo '<font size="3" color="#990000">' . "* $error" . "</font></br>";
        }    
    }
    elseif (empty($array) || !isset($array) )
    {
        echo "";
    } 

    elseif (empty($array) || !isset($array))
    {
        $array = array();
        $array = null;
    }

}

I've added the if condition to check if its empty, because sometimes I will pass an empty array.

This is a small part of these three files, it's supposed to be a registration form: index.php output.php and form_1.html.php All found here ( http://pastie.org/1062886 )

The index file checks if the user has filled in out the values and validates them, however if they haven't it'll place an error in the respective error array, the display error function is supposed to display them if there are values in the array passed to it.

I bet the solution is pretty basic, but I am a noob and its making me pull my hair out.

+8  A: 
<p><?php displayMessages($messages) ?></p>

That variable does not exist at that point in time.

<p><?php if(isset($messages)) displayMessages($messages); ?></p>

Calling isset later on in your function won't fix that: your argument has already been set to null (as $messages did not exist), and $array surely exists as a function argument.

Wrikken
You sir, are a great man! Thank you so much.
john mossel
@john mossel, make sure you click the check to the left of his post!
ItzWarty
Ah ok, if several people get it right do I check theirs aswell?
john mossel
Usually, the first one that got it right, or the most complete, your pick, but just a single one. As long as a correct answer is accepted for posterities sake I am not that bothered.
Wrikken
+1  A: 

It is complaining about $messages being undefined. I cannot see that variable in the snippet you posted, could you please post it?

I think you are actually doing something like this: displayMessages($messages). Make sure $messages exist:

if (isset($messages))
  displayMessages($messages);

Also empty() already checks if the variable is set, therefore the && !isset($array) checks are unnecessary in your code.

dark_charlie
The code is there like the OP said: http://pastie.org/1062881
Wrikken