Self-submitting Form Sample

Sometimes form sends its data for processing to the page containing this form (self-submit). For example forms created by Adobe Dreamweaver Server Behaviors behave like that. The following example demonstrates how to use VDaemon v3.x to work with self-submitting pages.

Run Self-submitting Form Sample

In common case of server-side validation VDaemon checks submited data on the action page and redirects visitor back to the form page if validation isn't passed. But redirection doesn't have a sense if action page is identical to the form page. In this case VDaemon doesn't break execution of the action page regardless of validation result. But your script must take an action according to this result. How can you know whether validation passed or not?

VDaemon provides validation results information in global variable $oVDaemonStatus. If validation didn't take place yet (page is accessed first time), its value is null. If validation have taken place $oVDaemonStatus is not null, it contains object of CVDFormStatus class (declared in vdaemon.php file):

$oVDaemonStatus->sForm - name of form which has been validated
$oVDaemonStatus->bValid - boolean value that indicates whether form validation passed
$oVDaemonStatus->aValidators- array of objects of CVDValidatorStatus class that contains information about every validator.

This information can be used to decide whether submitted data valid or not. Use following "if" statement to do this:

if ($oVDaemonStatus && $oVDaemonStatus->bValid) {
   // validation takes place and submitted data is valid
}

Run Self-submitting Form Sample

Form page source (selfsubmit_f.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);

include('vdaemon.php');

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

if ($oVDaemonStatus && $oVDaemonStatus->bValid)
{
    // Login successful.
    // Redirect to one of the "member's area" page.
    header("location: selfsubmit_p.php");
}
?>
<html>
<head>
<title>Self-submitting 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>Self-submitting Form Sample</h1>
<p>You can enter 'x-code' as User ID and any non-empty password to pass validation.<br>
  Or you can first register User ID you want on Registration Form sample page.</p>
<form action="selfsubmit_f.php" method="post" id="SelfSubmit" runat="vdaemon">
  <table border="0" cellpadding="2" cellspacing="0">
    <tr>
      <td colspan="2">
        <vllabel
          errtext="User ID or Password incorrect"
          validators="UserID,UserIDExist,Password"
          errclass="error">&nbsp;
        </vllabel>
      </td>
    </tr>
    <tr>
      <td width="80">User ID:</td>
      <td width="200">
        <input name="UserID" type="text" class="control" size="20">
        <vlvalidator name="UserID" type="required" control="UserID">
        <vlvalidator name="UserIDExist" type="custom" control="UserID" function="UserIDCheck">
      </td>
    </tr>
    <tr>
      <td>Password:</td>
      <td>
        <input name="Password" type="password" class="control" size="20">
        <vlvalidator type="required" name="Password" control="Password">
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input name="submit" type="submit" class="control" value="Login">
      </td>
    </tr>
  </table>
</form>
</body>
</html>
<?php VDEnd(); ?>

Confirmation page source (selfsubmit_p.php)

<html>
<head>
<title>Self-submitting 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>Self-submitting Form Sample</h1>
<p>Congratulation! You have successfully logged in.</p>
</body>
</html>