<?php
ob_start(); // allow redirect

require_once("includes/redirect.php");
include_once("includes/formverifier.php");

$title = "FormVerifier Test Page";
main($title);

function main($title = "") {
    $f = new FormVerifier();
    if (isset($_REQUEST["submitTest"])) {
        checkForm($f);
        if (!$f->isError()) { // data is OK
            processData($f);
            redirect("echo.php?it=worked");
        }
    }
    require("includes/header.php");
    showContent($title, $f);
    require("includes/footer.php");
}

// Check form for errors and return error messages
function checkForm(&$f) {
    // Code verification tests here
    $f->isEmpty('name', "You must enter a name");
    $f->isEmpty('state', "You must select a state");
    $f->isEmpty('lang', "You must select a language");
}

// Save the data
function processData($f) {
    // Save the data;
    $name = $f->getValue("name");
    $sex = $f->getValue("sex");
    $state = $f->getValue("state");
    $lang = $f->getValue("lang");
    $comments = $f->getValue("comments");

    include_once("includes/dbconvars.php");
    @$dbCnx = mysql_connect($dbhost, $dbuser, $dbpwd)
        or die("Could not connect");
    mysql_select_db($dbname, $dbCnx)
        or die(mysql_error());
    $sql = "
      INSERT INTO someTable(Name, Sex, State, Language, Comments )
      VALUES ('$name', '$sex', '$state', '$lang[0]', '$comments')
      ";

    // For debugging (remove when finished)
    echo "<pre>\n";
    print_r($_REQUEST);
    echo "</pre>\n";
    echo "<p>SQL: $sql</p>\n";
    die("Stopped for testing"); // stop the redirect

    mysql_query($sql) or die(mysql_error());
    mysql_close($dbCnx);
}

// Display the content of a page
function showContent($title, $f) {
?>
<h1><?php echo $title ?></h1>

<?php echo $f->reportErrors() ?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>"
  method="post">
<table>
<tr>
  <th align="left">Description</th>
  <th align="left">Entry</th>
</tr>
<tr>
  <td><?php echo $f->formatOnError('name', 'Name:') ?></td>
  <td><input type="text" name="name" size="15"
  value="<?php echo $f->getValue('name'); ?>">
  <?php echo $f->showMessageOnError('name') ?></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('sex', 'Sex:') ?></td>
  <td>
    <input type="radio" id="r1" name="sex" value="m"
      <?php
        $value = $f->getValue('sex');
        if ($value == 'm') echo " checked";
      ?>><label for="r1"> Male</label>
    <input type="radio" id="r2" name="sex" value="f"
      <?php
        $value = $f->getValue('sex');
        if ($value == 'f') echo " checked";
      ?>><label for="r2"> Female</label>
    <input type="radio" id="r3" name="sex" value="n"
      <?php
        $value = $f->getValue('sex', 'n');
        if ($value == 'n') echo " checked";
      ?>><label for="r3"> Not specfied</label>
  </td>
</tr>
<tr>
  <td><?php echo $f->showMessageOnError('state', 'State:') ?></td>
  <td>
    <select name="state" size="1">
    <option value="">- Select One -</option>
    <option value="CA"
      <?php
        $value = $f->getValue('state');
        if ($value == 'CA') echo " selected";
      ?>>California</option>
    <option value="OR"
      <?php
        $value = $f->getValue('state');
        if ($value == 'OR') echo " selected";
      ?>>Oregon</option>
    <option value="WA"
      <?php
        $value = $f->getValue('state');
        if ($value == 'WA') echo " selected";
      ?>>Washington</option>
    </select>
  <?php echo $f->showMessageOnError('state') ?></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('lang', 'Languages:') ?></td>
  <td><input type="checkbox" id="c1" name="lang[]" value="ASP"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('ASP', $value)) {
            echo " checked";
        }
     ?>><label for="c1"> ASP</label>
  <input type="checkbox" id="c2" name="lang[]" value="JSP"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('JSP', $value)) {
            echo " checked";
        }
     ?>><label for="c2"> JSP</label>
  <input type="checkbox" id="c3" name="lang[]" value="PHP"
     <?php
        $default = array('PHP');
        if (isset($_REQUEST["submitTest"])) $default = "";
        $value = $f->getValue('lang', $default);
        if ($value and is_array($value) and in_array('PHP', $value)) {
            echo " checked";
        }
     ?>><label for="c3"> PHP</label>
  <input type="checkbox" id="c4" name="lang[]" value="Other"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('Other', $value)) {
            echo " checked";
        }
     ?>><label for="c4"> Other</label>
  <?php echo $f->showMessageOnError('lang') ?></td>
</tr>
<tr>
  <td><i>Comments (Optional):</i></td>
  <td><textarea name="comments" rows="5" cols="45">
<?php echo $f->getValue('comments', 'Enter comments here'); ?>
  </textarea></td>
</tr>
<tr>
  <td><input type="submit" name="submitTest" value="Save"></td>
  <td>&nbsp;</td>
</tr>
</table>
</form>

<?php
}
?>