Example Code

This is the source code of the demo.

<?php
// you will have started a session somewhere in your loader, this is here for the example.
session_start();


// if the form has been submitted
if ( !empty($_POST['submit']) )
{
	// very basic error checking 
	
	// check if the verification code is correct
	if ( $_POST['vericode'] != $_SESSION['vericode'])
	{
		$errors[] = 'The verification code is incorrect.';
	}
	if ( empty($_POST['name']) )
	{
		$errors[] = 'Enter your name please.';
	}

	//  ...other error checking etc...
	
	// any errors?
	if ( !empty($errors)	 )
	{
		echo '<h3>Error!</h3>';
		foreach ($errors as $error)
		{
			echo $error .'<br/>';
		}
		form();
	}
	else
	{
		// all good, do your stuff and say thanks...
		echo 'all good';
	}

}
else
{
	// show your form...
	form();
}


// form in a function to simplify the example code.
function form()
{
	?>
	<form name="contactForm" id="contactForm" method="post" action="">
		
		<label for="name" class="required">Name:</label>
		<input type="text" name="name" id="name" value="" />
		
		<p>Please enter the verification code:</p>
		
		<label for="vericode" class="required">Vericode:</label>
		<input type="text" name="vericode" />
		<img src="/vericode-demo/vericode.php" alt="Vericode" />
		
		<input type="submit" name="submit" id="submit" value="Send" />
	
	</form>
	<?php
}
?>
back to top