<?php
ob_start(); // for 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["submit"])) {
        $error = checkForm($f);
        if (!$error) { // data is OK
            saveData($f);
            redirect("index.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");
    return $f->isError();
}

// Save the data
function saveData($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)
    var_dump($_REQUEST);
    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 method="POST" action="formtest.php">
<p><table>
<tr>
  <th align="left">Description</th>
  <th align="left">Entry</th>
</tr>
<tr>
  <td><?php echo $f->formatOnError('name', '<i>Name:</i>') ?></td>
  <td><input type="text" name="name" size="15"
    value="<?php echo $f->getValue('name'); ?>"></td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('sex', '<i>Sex:</i>') ?></td>
  <td>
    <input type="radio" name="sex" value="male"
      <?php
        $value = $f->getValue('sex');
        if ($value == 'male') echo " checked";
      ?>> Male
    <input type="radio" name="sex" value="female"
      <?php
        $value = $f->getValue('sex', 'female');
        if ($value == 'female') echo " checked";
      ?>> Female
  </td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('state', '<i>State:</i>') ?></td>
  <td>
    <select name="state" size="1">
    <option value="">- Select One -
    <option value="CA"
      <?php
        $value = $f->getValue('state');
        if ($value == 'CA') echo " selected";
      ?>>California
    <option value="OR"
      <?php
        $value = $f->getValue('state');
        if ($value == 'OR') echo " selected";
      ?>>Oregon
    <option value="WA"
      <?php
        $value = $f->getValue('state');
        if ($value == 'WA') echo " selected";
      ?>>Washington
    </select>
  </td>
</tr>
<tr>
  <td><?php echo $f->formatOnError('lang', '<i>Languages:</i>') ?></td>
  <td><input type="checkbox" name="lang[]" value="ASP"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('ASP', $value)) {
            echo " checked";
        }
     ?>> ASP
  <input type="checkbox" name="lang[]" value="JSP"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('JSP', $value)) {
            echo " checked";
        }
     ?>> JSP
  <input type="checkbox" name="lang[]" value="PHP"
     <?php
        $value = $f->getValue('lang', array('PHP'));
        if ($value and is_array($value) and in_array('PHP', $value)) {
            echo " checked";
        }
     ?>> PHP
  <input type="checkbox" name="lang[]" value="Other"
     <?php
        $value = $f->getValue('lang');
        if ($value and is_array($value) and in_array('Other', $value)) {
            echo " checked";
        }
     ?>> Other
  </td>
</tr>
<tr>
  <td><p><i>Comments (Optional):</i></td>
  <td><textarea name="comments" rows="5" cols="50">
Enter comments here</textarea>
</td>
</tr>
<tr>
  <td><input type="submit" name="submit" value="Save"></td>
  <td>&nbsp;</td>
</tr>
</table></p>
</form>

<?php
}
?>