0

I have two PHP files.

The first (main_form.php) contains the HTML form and displays the errors if the form is incorrect. The errors are obtained for the second file.

The second file called validate.php has the following code to display errors:

    foreach ($error as $errorKeys) {
        $_SESSION['errors'] =  $errorKeys;
        echo $_SESSION['errors'];
    }
    header('Location: ../main_form.php');

If the header('Location: ../main_form.php'); is left out the errors display correctly.

The first file (main_form.php) has the following code to get the errors (if any) from validate.php:

echo '<ul>';
foreach ($_SESSION['errors'] as $errorKeys) {
    //$errorKeys = $_SESSION['errors'];
    echo $errorKeys;
}
echo '<ul>';

I am getting the following error in the error_log : PHP Warning: Invalid argument supplied for foreach()

The errors are in the form of arrays e.g. $error['business_website'] = Address is incorrect.

How can i parse the errors from validate.php to main_form.php please?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
user3173207
  • 269
  • 1
  • 7
  • 21
  • Try: `$_SESSION['errors'][] = $errorKeys;` – The Blue Dog Feb 11 '14 at 19:51
  • `$_SESSION['errors'] = $errorKeys;` --- you're overwriting the same element with the scalar value here – zerkms Feb 11 '14 at 19:51
  • First problem: you can't call `header` after output to the browser (the `echo` line). Second problem, your errors key isn't an array, thus is can't loop over it. In your first script you are setting it as a string potentially multiple times. – scrowler Feb 11 '14 at 19:51
  • possible duplicate of [Invalid argument supplied for foreach()](http://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Félix Adriyel Gagnon-Grenier Jul 07 '15 at 18:58

0 Answers0