Jump to content

Recommended Posts

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

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.

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

The problem is that you're trying to put an Array into the url, which you can't do.. because the URL is string based.  If you really want to pass the errors through the URL like that you'll have to

 

- serialize the array

- base64_encode it

 

Then on the other page just

- base64_decode it

- and unserialize it

 

 

@ zanus.  well i tried your suggestion.  I imploded the errors array on page 2 and url encoded it before passing it into the url like so:


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

        
    }
}

 

 

Then on page 1, I decoded the url and reconverted the string into my original errors arryay.


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

 

 

Well no error messages got displayed this time, but the register errors didn't get printed either.  What am I not doing right this time?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.