Need help transferring error messages to another page

Im trying to create a page that collects personal info from users and registers them. I have a page that contains the registration form which i will call page 1. A second page which I will call page 2, processes the form. A simplified version of page 2 looks like this[php]

<?php if (isset($_POST['submitted'])) { $errors = array(); // Connect to the database. require_once ('config.php'); //Check for errors. //Check to ensure that the password is long enough and is of the right format. if (eregi ("^[[:alnum:]]{8,16}$" , $_POST['password'])) { $b = TRUE; } else { $b = FALSE; $errors[] = 'Please enter a password that consists only of letters and numbers between 8 and 16 characters long.'; } //Check to make sure the password matches the confirmed password. if ($_POST['password'] == $_POST['password2']) { $c = TRUE; $password = $_POST['password']; //Encrypt the password. } else { $c = FALSE; $errors[] = 'The password you entered did not match the confirmed password.'; } //Check to make sure they entered their first name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['firstname'])) { $d = TRUE; } else { $d = FALSE; $errors[] = 'Please enter a valid first name.'; } //Check to make sure they entered their last name and it's of the right format. if (eregi ("^([[:alpha:]]|-|')+$", $_POST['lastname'])) { $e = TRUE; } else { $e = FALSE; $errors[] = 'Please enter a valid last name.'; } //Insert data into database. if (empty($errors)) { //query the database. for the sake of simplicity, I wont include the code // Show thank you message echo "

Thank You!

You have been registered"; } else { echo 'You could not be registered, please contact us about the problem and we will fix it as soon as we can.'; } //Display error messages. } else {// if errors array is not empty header("Location: page1.php?message=$errors");//Trying to pass the errors array into the url for page1 } } ?>

[/php]

So the idea is to create an array that contains all the errors. Upon querying the database, if the user completed the fields as required, a thank you message is printed. If not, he is redirected to the page containing the form (page 1) and the array containing the errors is transferred to page 1. Now I will include the relevant section of page 1 whihc is supposed to print the error messages.

[php]

<?php if(isset($_GET['message']) { echo '

Error!

The following error(s) occured:
'; foreach ($errors as $msg) { echo "$msg
\n"; } } ?>

[/php]

I get the following error message when I submit the registration forms with deliberate errors:

Error!

The following error(s) occured:

PHP Error Message

Warning: Invalid argument supplied for foreach() in /home/a4993450/public_html/register_page_content.php on line 66

Free Web Hosting

Can anyone point out why this error occurs?

Hi there,

An invalid foreach usually means that the variable is either not set or isn’t an array.

Well someone suggested that I convert the errors array to a string, url encode the string b4 passing it to the url on page 2. Then on the form page (page 1), decode the url and and reconvert to an array. I did that and this time, there is no php error but the error messages from the form aren’t printed. Can anyone tell me what I did wrong again? Here are the changes I made to page 2 and 1 respectively:[code]

} else {// if errors array is not empty

    //Conver the errors array into a string
    
    $errors_string = implode(" ", $errors);

    //Encode the imploded string.
    
    $message = urlencode($errors_string);


    header("Location: register_page.php?message=$message");

    
}

[/code]



<div id="error">


                                        <?php   if(isset($_GET['message'])) {

                                                //Decode the url
                                                
                                                $errors_string = urldecode($message);

                                                //Explode the decoded errors string back to an array.
                                        
                                                $errors = explode(" ", $errors_string);

                                                echo '<h3>Error!</h3>
                                                The following error(s) occured:<br />';

       
                                                foreach ($errors as $msg) {
                                                echo "$msg<br/>\n";
                                                }

                                          }

                                        ?>

                                        </div> <!--closes error-->

Unless I’m mistaken, you need to replace (in the second code snippet) urldecode($message) with urldecode($_GET[‘message’])

@smokey. thank u very much. that worked. problem solved :slight_smile:

Ok one more problem which i cant seem to resolve. I don’t know if it’s my html formatting. The error mesages get displayed, one word per line. I actually intended to display each error per line, hence the code
foreach ($errors as $msg) {
echo “$msg
\n”;
}
Any suggestion?

Sponsor our Newsletter | Privacy Policy | Terms of Service