Previous Page

Process a form and store user errors in an array for output.

NOTE: This is a sample of some things to check for when a user clicks the Sign Up button after entering information in a form, if they even enter anything. All errors encountered are stored in an array for later output.

Click on the following link to try it out. Try me!



You can insert the following code above the html portion of your new .php document

<?php

session_start();

if($_POST['submit']=='Sign Up')
{//1

	$err = array();// This array will store all errors encountered
	
	if( empty($_POST['name']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['confirm_password'])
	   || empty($_POST['email'])|| empty($_POST['confirm_email']))// make sure all fields that are not optional are filled in
	{
		$err[] ='You must fill in all fields.';	
	}
			
    if(!count($err))// if no error from above. All fields have something in them
    {
//====================================================================================== CHECK FOR INDIVIDUAL ERRORS ===========		
			if(strlen($_POST['username'])<4 || strlen($_POST['username'])>32)
			{
				$err[]='Your username must be between 4 and 32 characters!';
			}
			
			if(strlen($_POST['password'])<4 || strlen($_POST['password'])>32)
			{
				$err[]='Your password must be between 4 and 32 characters!';
			}
			
			if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['name']))
			{
				$err[]='Your name contains invalid characters!';
			}
			
			if(preg_match('/[^a-z0-9\-\_\.]+/i',$_POST['username']))
			{
				$err[]='Your username contains invalid characters!';
			}
		
			if($_POST['password'] != $_POST['confirm_password'])
			{
				$err[]='Your passwords do not match!';	
			}
			
			if($_POST['email'] != $_POST['confirm_email'])
			{
				$err[]='Your emails do not match!';	
			}
//===============================================================================================================================
			
			if(!count($err)) // if there are no errors, at all, then a success message can be stored for output
			{
				$_SESSION['message']['success'] ='Thank you! You have successfully filled out the form.';
			}		
		
    }
	
	if(count($err))// if there ARE errors
	{
		// take all the errors from the array and get them ready for output. This will kind of stack the errors by applying a line
		// break in between each one. You will see what I mean when you try out the form.....try to get as many errors as you can
		// to see how this works
		
		$_SESSION['message']['error'] = implode('
',$err); } } ?>

Below is the html portion....a sample sign up form. NOTE: Contains some CSS styling that comes before the body and head tags, so don't just place all this code between the body tags.

Also, take notice of the php code embedded in the input sections of the form. This is so, if there are errors that need fixed by the user, everything that they typed will still be in the fields after they click Sign Up.


<style type="text/css">
#form {
	position:relative;
	margin-left:auto;
	margin-right:auto;
	margin-top:25px;
	height: auto;
	width: 255px;
	padding:40px;
	text-align:left;
	color:#069;
		
	 box-shadow: 2px 1px 25px #000000;
    -moz-box-shadow: 2px 1px 25px #000000;
    -webkit-box-shadow: 2px 1px 25px #000000;
	
	 border-radius: 15px;
     -moz-border-radius: 15px;
     -webkit-border-radius: 15px;
     o-border-radius: 15px;
}
h1{
	color:#333;	
}
label{
		font-size:1.5em;
		font-weight:bold;	
}
input{
	background-color:#CCC;
	color:#006;
	width:250px;
    height:40px;
	font-size:1.4em;
   
}
#messages {
   
	margin:auto;
	height: auto;
	background:#FFC;
	width: 500px;
	font-size:1.4em;
	text-align:left;
	margin-top:25px;
	 
     box-shadow: 2px 1px 25px #000000;
    -moz-box-shadow: 2px 1px 25px #000000;
    -webkit-box-shadow: 2px 1px 25px #000000;

}
</style>

</head>

<body>	
   <div id="form">		
		<form action="" method="post">
			<h1>- Sign up form -</h1>	         
          
            <label for="name">Name:</label>
			<input  type="text" name="name" id="name" value="<?php if(count($err)) echo isset($_POST['name']) ? $_POST['name'] : "" ?>" size="23"  />
                    		
			<label  for="username">Username:</label>
			<input  type="text" name="username" id="username" value="<?php if(count($err)) echo isset($_POST['username']) ? $_POST['username'] : "" ?>" size="23"  />
                    
            <label  for="password">Password:</label>
			<input  type="password" name="password" id="password" value="<?php if(count($err)) echo isset($_POST['password']) ? $_POST['password'] : "" ?>" size="23" />
                    
            <label  for="confirm_password">Confirm Password:</label>
			<input  type="password" name="confirm_password" id="confirm_password" value="<?php if(count($err)) echo isset($_POST['confirm_password']) ? $_POST['confirm_password'] : "" ?>" size="23"  />
                    
			<label  for="email">Email:</label>
			<input  type="email" name="email" id="email" value="<?php if(count($err)) echo isset($_POST['email']) ? $_POST['email'] : "" ?>" size="23"   />
                    
            <label  for="confirm_email">Confirm Email:</label>
			<input  type="email" name="confirm_email" id="confirm_email" size="23" value="<?php if(count($err)) echo isset($_POST['confirm_email']) ? $_POST['confirm_email'] : "" ?>"  />
					
			<input type="submit" name="submit" value="Sign Up"  style="width:165px; height:40px; font-size:1.3em; margin-top:15px;"/>
		</form>
				
   </div>

Below is the php to output the errors or success message. You can place this where you want it to display

 
   <div id="messages" >
                 <?php
						
						if($_SESSION['message']['error'])// if there WERE errors, this will output them, then destroy the variable
						{
							echo '<div  style="color:red;  border: 1px solid #ff0000;">'.$_SESSION['message']['error'].'</div>';
							unset($_SESSION['message']['error']);
						}
						
						if($_SESSION['message']['success'])// if there were NO errors, this will output the variable's contents, then destroy the variable
						{
							echo  '<div  style="color:green; border: 1px solid #04ef14; padding:15px;">'.$_SESSION['message']['success'].'</div>';
							unset($_SESSION['message']['success']);
						}
					?>
    </div>