Registration Form Sample

It is simple user registration form. User Id is verified against database. If validation passed submitted data (actually User ID only) is added to the database. The sample demonstrates how VDaemon sorts out most common validation tasks.

Run Registration Form Sample

You can see bellow source code of two pages. First page contains form that must be validated. I call it "form page". Second page contains code for processing user input. Address of this page is specified in the "action" attribute of validated form. I call this page "action page" or "processing page".

All VDaemon-specific code is highlighted using bold font. Draw attantion to the following points:

- All properly written VDaemon pages (both form and action) must begin from include('vdaemon.php'); statement. Of course you must specify a right server path to vdaemon.php file when you will write your pages. Form page must end with VDEnd(); function call.

- To say VDaemon that it must check the form you need to add runat="vdaemon" attribute to the <form> tag. Form "id" or "name" attribute also must be defined and unique.

- VDaemon can automatically disable all or only submit form buttons before sending data to the server to prevent visitor to submit form twice. Add disablebuttons="all" or disablebuttons="submit" attribute to the <form> tag to turn this on.

- Validation rules are defined by <vlvalidator> tags. For example
<vlvalidator name="UserID" type="required" control="UserID" errmsg="User ID required">
checks that visitor entered some data to the "UserID" field. And validator
<vlvalidator type="format" format="email" name="Email" control="Email" errmsg="Invalid E-mail">
checks "Email" field to be a valid e-mail address.

- Validator can be evaluated to true (user data is valid for this validation rule) or to false (user data is invalid). Each validator can define error message in the "errmsg" attribute. Error messages from all evaluated to false validators can be displayed in one place by summary:
<vlsummary class="error" headertext="Error(s) found:" displaymode="bulletlist">

- Another way to display errors to visitor is using VDaemon labels:
<vllabel errclass="error" validators="NameReq,NameRegExp" for="Name" cerrclass="controlerror">Name:</vllabel>
When VDaemon parses page it replaces all <vllabel> VDaemon tags to <label> html tags. You can use any <label> attribute inside <vllabel> tag - it will be kept after replacement. VDaemon label state depends on one or several validators. Label is displayed differently in error state - it can change style and inner text. "errclass" attribute defines style for error state. "errtext" attribute defines alternative inner text. When VDaemon label is in error state it also can change style of the form input element. "for" attribute defines input element to change (you can reference input element "name" or "id" attribute). "cerrclass" attribute defines input element error class.

Run Registration Form Sample

Form page source (registration_f.php)

<?php include('vdaemon.php'); ?>
<html>
<head>
<title>Registration Form Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Registration Form Sample</h1>
<form id="Register" action="registration_p.php" method="POST" runat="vdaemon" disablebuttons="all">
  <table cellpadding="2" cellspacing="0" border="0">
    <tr>
      <td width="130">
        <vllabel validators="UserID,UserIDExist" errclass="error" for="UserID" cerrclass="controlerror">User ID:</vllabel>
      </td>
      <td width="140">
        <input name="UserID" type="text" class="control" id="UserID" size="15">
        <vlvalidator name="UserID" type="required" control="UserID" errmsg="User ID required">
        <vlvalidator name="UserIDExist" type="custom" control="UserID" errmsg="User ID already exist" function="UserIDCheck">
      </td>
      <td width="300" rowspan="7" valign="top">
        <vlsummary class="error" headertext="Error(s) found:" displaymode="bulletlist">
      </td>
    </tr>
    <tr>
      <td>
        <vllabel errclass="error" validators="Password,PassCmp" for="Password" cerrclass="controlerror">Password:</vllabel>
      </td>
      <td>
        <input name="Password" type="password" class="control" id="Password" size="15">
        <vlvalidator type="required" name="Password" control="Password" errmsg="Password required">
        <vlvalidator name="PassCmp" type="compare" control="Password" comparecontrol="Password2"
          operator="e" validtype="string" errmsg="Both Password fields must be equal">
      </td>
    </tr>
    <tr>
      <td>
        <vllabel validators="Password,PassCmp" errclass="error" for="Password2" cerrclass="controlerror">Confirm Password:</vllabel>
      </td>
      <td>
        <input name="Password2" type="PASSWORD" class="control" id="Password2" size="15">
      </td>
    </tr>
    <tr>
      <td>
        <vllabel errclass="error" validators="NameReq,NameRegExp" for="Name" cerrclass="controlerror">Name:</vllabel>
      </td>
      <td>
        <input name="Name" type="text" class="control" id="Name" size="15">
        <vlvalidator type="required" name="NameReq" control="Name" errmsg="Name required">
        <vlvalidator type="regexp" name="NameRegExp" control="Name" regexp="/^[A-Za-z'\s]*$/" errmsg="Invalid Name">
      </td>
    </tr>
    <tr>
      <td>
        <vllabel errclass="error" validators="EmailReq,Email" for="Email" cerrclass="controlerror">E-mail:</vllabel>
      </td>
      <td>
        <input name="Email" type="TEXT" class="control" id="Email" size="15">
        <vlvalidator type="required" name="EmailReq" control="Email" errmsg="E-mail required">
        <vlvalidator type="format" format="email" name="Email" control="Email" errmsg="Invalid E-mail">
      </td>
    </tr>
    <tr>
      <td colspan=2>
        <input name="Agreement" type="checkbox" id="Agreement" value="1">
        <vllabel errclass="error" validators="Agreement" for="Agreement">I agree with the terms of service</vllabel>
        <vlvalidator type="required" name="Agreement" control="Agreement" errmsg="Agreement checkbox must be selected">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" class="control" value="Register">
        <input type="reset" class="control" value="Reset">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<?php VDEnd(); ?>

Action page source (registration_p.php)

<?php
$sDatabase = 'db_name';
$sHostname = 'localhost';
$sPort     = 3306;
$sUsername = 'user_name';
$sPassword = 'password';
$sTable    = 'Customers';

$rConn = mysql_connect("$sHostname:$sPort", $sUsername, $sPassword) or die(mysql_error());
mysql_select_db($sDatabase);

define('VDAEMON_PARSE', false);
include('vdaemon.php');

function UserIDCheck($sValue, &$oStatus)
{
    global $sTable;
    $sUserID = addslashes($sValue);
    
    $oStatus->bValid = false;
    $oStatus->sErrMsg = "User ID '$sValue' already exist";
    
    $sQuery = "SELECT UserID FROM $sTable WHERE UserID = '$sUserID'";
    if ($rRecordset = mysql_query($sQuery))
    {
        $oStatus->bValid = mysql_num_rows($rRecordset) == 0;
        mysql_free_result($rRecordset);
    }
}

// VDFormat - VDaemon function for adding or removing slashes
$sUserID = VDFormat($_POST['UserID'], true);
$sQuery = "INSERT INTO $sTable SET UserID = '$sUserID'";
mysql_query($sQuery);
?>
<html>
<head>
<title>Registration Form Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="samples.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Registration Form Sample</h1>
<p>Your data has been written to the database</p>
<table cellpadding="0" cellspacing="0" border="0">
  <tr>
    <td width="100">User ID:</td>
    <td width="300"><?php echo $_POST['UserID']; ?></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><?php echo $_POST['Password']; ?></td>
  </tr>
  <tr>
    <td>Name:</td>
    <td><?php echo $_POST['Name']; ?></td>
  </tr>
  <tr>
    <td>E-mail:</td>
    <td><?php echo $_POST['Email']; ?></td>
  </tr>
</table>
</body>
</html>