There are many different ways to do that. Here’s one way.
register.php
<?php
require_once 'RegisterModel.php';
require_once 'RegisterDB.php';
require_once 'RegisterView.php';
$contentDB = new RegisterDB();
$contentView = new RegisterView();
$contentModel = new RegisterModel($contentDB, $contentView);
echo $contentModel->render();
?>
RegisterModel.php
<?php
class RegisterModel
{
private $_db;
private $_view;
private $_userId;
private $_error;
public function __construct($db, $view)
{
$this->_db = $db;
$this->_view = $view;
$this->_userId = 0;
$this->_error = array();
}
private function getAction()
{
$action = '';
if (isset($_POST['createAccount'])) {
$action = 'create';
}
if (isset($_POST['personalDetails'])) {
$action = 'personal';
}
return $action;
}
function getAccountDetails()
{
return array(
'username' => filter_input(INPUT_POST, 'username', FILTER_SANITIZE_SPECIAL_CHARS),
'password' => filter_input(INPUT_POST, 'password', FILTER_SANITIZE_SPECIAL_CHARS),
'confirmPassword' => filter_input(INPUT_POST, 'confirmPassword', FILTER_SANITIZE_SPECIAL_CHARS)
);
}
function getPersonalDetails()
{
return array(
'userid' => filter_input(INPUT_POST, 'userid', FILTER_SANITIZE_SPECIAL_CHARS),
'first' => filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_SPECIAL_CHARS),
'last' => filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_SPECIAL_CHARS),
'dob' => filter_input(INPUT_POST, 'dob', FILTER_SANITIZE_SPECIAL_CHARS),
'gender' => filter_input(INPUT_POST, 'gender', FILTER_SANITIZE_SPECIAL_CHARS),
'email' => filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL),
'country' => filter_input(INPUT_POST, 'country', FILTER_SANITIZE_SPECIAL_CHARS)
);
}
private function processAction($action)
{
$db = $this->_db;
$success = FALSE;
switch ($action) {
case 'create':
$userDetails = $this->getPersonalDetails();
list($username, $password, $confirmPassword) = $userDetails;
if ($password === $confirmPassword) {
$passcode = crypt($password);
$userId =
$this->_userId = $db->addUser($username, $passcode);
$success = ($this->_userId > 0);
} else {
array_push($this->_error, 'password');
}
break;
case 'personal':
$details = $this->getPersonalDetails();
list($userId, $first, $last, $dob, $gender, $email, $country) = $userDetails;
$this->_userId = $userId;
$updated = $db->addPersonalDetails($userId, $first, $last, $dob, $gender, $email, $country);
$success = ($updated > 0);
if (!$success) {
array_push($this->_error, 'personal');
}
}
return $success;
}
private function countries()
{
$view = $this->_view;
$list = '';
$countries = array('New Zealand', 'Other');
return $view->options($countries);
}
private function getView($action)
{
$view = $this->_view;
$content = '';
switch ($action) {
case 'personal':
$content .= $view->thankyou();
break;
case 'create':
if (isset($_error['password'])) {
$content .= $view->passwordError();
$content .= $this->getView();
} else {
$countries = $this->countries();
$content .= $view->userDetails($this->_userId, $countries);
}
break;
default:
$content .= $view->createAccount();
}
return $content;
}
public function render()
{
$action = $this->getAction();
$success = $this->processAction($action);
$content = $this->getView($action);
return $content;
}
}
?>
RegisterView.php
<?php
class RegisterView
{
public function options($names)
{
$options = '';
foreach ($names as $name => $value) {
$options .= '<option name="' . $name . '">' . $value . '</option>';
}
return $options;
}
public function createAccount()
{
return <<< EOT
<h1>Register</h1>
<h2>Step 1 of 2</h2>
<ol>
<li><strong>Create account</strong></li>
<li>User details</li>
</ol>
<form method="post">
<p><label>Username: <input type="text" name="username"></label></p>
<p><label>Password: <input type="password" name="password"></label></p>
<p><label>Confirm Password: <input type="password" name="confirmPassword"></label></p>
<p><input type="submit" name="createAccount" value="Create account"></p>
</form>
EOT;
}
public function passwordError()
{
return '<p>There was a problem with your password.</p>';
}
public function userDetails($userId, $countries)
{
return <<< EOT
<h1>Register</h1>
<h2>Step 2 of 2</h2>
<ol>
<li>Create account</li>
<li><strong>User details</strong></li>
</ol>
<form method="post">
<p><label>First Name: <input type="text" name="firstname"></label></p>
<p><label>Last Name: <input type="text" name="lastname"></label></p>
<p><label>Date of Birth: <input type="text" name="dateofBirth"></label></p>
<p><label>Gender:
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
</label></p>
<p><label>Alternate Email: <input type="text" name="email"></label></p>
<p><label>Country:
<select name="country">$countries</select>
</label></p>
<p>
<input type="hidden" name="userid" value="$userId">
<input type="submit" name="personalDetails" value="Update my details">
</p>
</form>
EOT;
}
public function thankyou()
{
return <<< EOT
<h1>Register</h1>
<h2>Thank You</h2>
EOT;
}
public function page($content)
{
return <<< EOT
<html>
<head>
</head>
</head>
<body>
$content
</body>
</HTML>
EOT;
}
}
?>
and the database, I’ll leave for you to implement according to however you desire.
RegisterDB.php
<?php
class RegisterDB
{
public function addUser($username, $password)
{
return 3; // last insert id
}
public function addPersonalDetails($userId, $first, $last, $dob, $gender, $email, $country)
{
return 1; // row updated
}
}
?>