Need help transferring error messages to another page
Posted: Sat May 14, 2011 4:26 pm
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
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.
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?
Code: Select all
<?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 "<h3>Thank You!</h3>
You have been registered";
} else {
echo '<font color="red">You could not be registered, please contact us about the problem and we will fix it as soon as we can.</font>';
}
//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
}
}
?>
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.
Code: Select all
<?php if(isset($_GET['message']) {
echo '<h3>Error!</h3>
The following error(s) occured:<br />';
foreach ($errors as $msg) {
echo "$msg<br/>\n";
}
}
?>
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?