tags:

views:

47

answers:

5

Done....moderators.close this post...Thanks everyone for helping me out.

+3  A: 

There is no array named $errors. You will have to look further up your script why not.

You can fix the error message by using

if (!empty($errors) and (is_array($errors)))
 foreach ($errors as $msg)
Pekka
I have set errors....here's a preview of my code above... if(isset($_POST['submitted'])) { require_once('C:\wamp\www\connect.php'); //connecting to db $errors = array(); if (empty($_POST['email'])) { $errors[]='Please enter a valid email address.'; }
Johnson
@Johnson strange. Can you post the full code as it is, in a separate code block?
Pekka
i have posted the complete code.thanks
Johnson
@Johnson you are doing the `$errors = array();` at the wrong place, inside the condition checking for `isset($_POST['submitted'])`. You need to take it outside into the main script
Pekka
if i do that, the else part of Err...the following error(s) have occured is still displayed, but the errors are gone.
Johnson
@Johnson that is because you are showing that block when `$_POST["submitted"]` is not set. Doesn't sound like that's what you want to do
Pekka
how do i fix that?
Johnson
@Johnson as far as I can see, you are missing a `{` after ` if(empty($errors))`. You need to get your if/else blocks right
Pekka
Whoa....thanks pekka..the errors are gone..ur amazing.
Johnson
A: 

Basically, what's happening here is you're using $errors before it is defined.

It may be that you need to set "$errors = array( )" near the top of your script so that it is always at least an empty array.

dpk
+2  A: 

Your foreach loop is out of the scope in regards to where the $error array is defined.

Your code in a nutshell:

if(isset($_POST['submitted'])) {
    $errors = array();
} else {
    foreach($errors as $error)
}

If $_POST is not set, than your $errors is not defined.

Russell Dias
+2  A: 

Move your declaration for "$errors = array()" above the line "if(isset($_POST['submitted'])) { " and everything should work fine!

deepsat
A: 

You have two problems. The first is the cause of the empty/non-existent array and the second is a lack of testing for it.

The first is that you are testing for errors inside of an if block and then looping through them inside of the else block.

if (isset($_POST['submitted'])) {
  // create errors array and set errors
} else {
  // loop through array of errors
}

So if errors are set, the script doesn't make it to the loop. If the script makes it to the loop, no errors were set.

The second is that you should only enter the foreach loop after you have tested the array:

if (!empty($errors) && is_array($errors)) { // use this line and get rid of the else.
  foreach ($errors as $msg) {
        echo "--$msg<br />\n";
  }
  echo '</p><p>Please Try Again.</p><p><br/></p>';
 } // and close it.
Ted