Page 1 of 1

Need help transferring error messages to another page

Posted: Sat May 14, 2011 4:26 pm
by drayarms
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

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";
        					}

					  }

					?>
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?

Re: Need help transferring error messages to another page

Posted: Sat May 14, 2011 6:12 pm
by Christopher
A better practice for forms is to have the page submit to itself. Then upon success redirect to a success page. That also solves the multiple-submit problem for forms.

Re: Need help transferring error messages to another page

Posted: Sun May 15, 2011 4:36 am
by drayarms
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: Select all

} 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: Select all


<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-->

Re: Need help transferring error messages to another page

Posted: Sun May 15, 2011 1:36 pm
by flying_circus
drayarms wrote:Can anyone tell me what I did wrong again?
In my opinion, you were steered in the wrong direction. Follow Christopher's advice. Have the form submit to itself (same location). Using PHP, if there are errors output the errors else output the form. Do not mess with moving stuff back and forth in the url querystring. It's poor coding practice and any user can simply modify the url querystring to make the errors say whatever they want. If you MUST pass between pages, a session is likely the better tool, but I still think Christopher's advice is superior.